diff --git a/src/mi_note/app/src/main/AndroidManifest.xml b/src/mi_note/app/src/main/AndroidManifest.xml index c1abf9e..8d69468 100644 --- a/src/mi_note/app/src/main/AndroidManifest.xml +++ b/src/mi_note/app/src/main/AndroidManifest.xml @@ -23,7 +23,11 @@ tools:ignore="GradleOverrides"> - + + + + @@ -34,6 +38,8 @@ + diff --git a/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java index 99d1ecf..18c2d6e 100644 --- a/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java +++ b/src/mi_note/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java @@ -16,6 +16,7 @@ package net.micode.notes.ui; +import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlarmManager; import android.app.AlertDialog; @@ -27,7 +28,13 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ShortcutInfo; +import android.content.pm.ShortcutManager; +import android.graphics.BitmapFactory; import android.graphics.Paint; +import android.graphics.drawable.Icon; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.Spannable; @@ -52,7 +59,11 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.pm.ShortcutInfoCompat; +import androidx.core.content.pm.ShortcutManagerCompat; +import androidx.core.graphics.drawable.IconCompat; import net.micode.notes.R; import net.micode.notes.data.Notes; @@ -67,8 +78,10 @@ import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener; import net.micode.notes.widget.NoteWidgetProvider_2x; import net.micode.notes.widget.NoteWidgetProvider_4x; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -427,6 +440,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen setResult(RESULT_OK, intent); } + @SuppressLint("WrongConstant") public void onClick(View v) { int id = v.getId(); if (id == R.id.btn_set_bg_color) { @@ -507,6 +521,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen return true; } + @RequiresApi(api = Build.VERSION_CODES.O) @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -823,6 +838,8 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen return saved; } + + @RequiresApi(api = Build.VERSION_CODES.O) private void sendToDesktop() { /** * Before send message to home, we should make sure that current @@ -834,7 +851,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen } if (mWorkingNote.getNoteId() > 0) { - Intent sender = new Intent(); + /*Intent sender = new Intent(); Intent shortcutIntent = new Intent(this, NoteEditActivity.class); shortcutIntent.setAction(Intent.ACTION_VIEW); shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId()); @@ -843,11 +860,59 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen makeShortcutIconTitle(mWorkingNote.getContent())); sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app)); - sender.putExtra("duplicate", true); sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); + sender.putExtra("duplicate", true); showToast(R.string.info_note_enter_desktop); + System.out.println("创建快捷方式"); sendBroadcast(sender); - } else { + System.out.println("创建完成");*/ + + + /*Intent shortcutIntent = new Intent(this, NoteEditActivity.class); + shortcutIntent.setAction(Intent.ACTION_VIEW); + shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId()); + ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, String.valueOf(mWorkingNote.getNoteId())) + .setShortLabel(makeShortcutIconTitle(mWorkingNote.getContent())) + .setLongLabel(makeShortcutIconTitle(mWorkingNote.getContent())) + .setIcon(IconCompat.createWithResource(this, R.drawable.icon_app)) + .setIntent(shortcutIntent) + .build(); + List lst = new ArrayList<>(); + lst.add(shortcut); + ShortcutManagerCompat.addDynamicShortcuts(this, lst);*/ + + ShortcutManager shortcutManager = + getSystemService(ShortcutManager.class); + + if (shortcutManager.isRequestPinShortcutSupported()) { + // Assumes there's already a shortcut with the ID "my-shortcut". + // The shortcut must be enabled. + Intent shortcutIntent = new Intent(this, NoteEditActivity.class); + shortcutIntent.setAction(Intent.ACTION_VIEW); + shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId()); + ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(this, String.valueOf(mWorkingNote.getNoteId())) + .setShortLabel(makeShortcutIconTitle(mWorkingNote.getContent())) + .setLongLabel(makeShortcutIconTitle(mWorkingNote.getContent())) + .setIcon(Icon.createWithResource(this, R.drawable.icon_app)) + .setIntent(shortcutIntent) + .build(); + // Create the PendingIntent object only if your app needs to be notified + // that the user allowed the shortcut to be pinned. Note that, if the + // pinning operation fails, your app isn't notified. We assume here that the + // app has implemented a method called createShortcutResultIntent() that + // returns a broadcast intent. + Intent pinnedShortcutCallbackIntent = + shortcutManager.createShortcutResultIntent(pinShortcutInfo); + + // Configure the intent so that your app's broadcast receiver gets + // the callback successfully.For details, see PendingIntent.getBroadcast(). + PendingIntent successCallback = PendingIntent.getBroadcast(this, 0, + pinnedShortcutCallbackIntent, 0); + + shortcutManager.requestPinShortcut(pinShortcutInfo, + successCallback.getIntentSender()); + } + } else { /** * There is the condition that user has input nothing (the note is * not worthy saving), we have no note id, remind the user that he diff --git a/src/mi_note/app/src/main/res/values/styles.xml b/src/mi_note/app/src/main/res/values/styles.xml index d750e65..ad0d90d 100644 --- a/src/mi_note/app/src/main/res/values/styles.xml +++ b/src/mi_note/app/src/main/res/values/styles.xml @@ -64,6 +64,6 @@ \ No newline at end of file