Add pin shortcut callback

This commit is contained in:
vvb2060 2022-12-05 00:35:36 +08:00 committed by 南宫雪珊
parent 26dd523fa0
commit b9c470f7cd
3 changed files with 49 additions and 13 deletions

View File

@ -23,7 +23,7 @@ public class WelcomeDialog extends DialogFragment {
.setNegativeButton(R.string.never_show, (dialog, which) -> .setNegativeButton(R.string.never_show, (dialog, which) ->
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply()) App.getPreferences().edit().putBoolean("never_show_welcome", true).apply())
.setNeutralButton(R.string.create_shortcut, (dialog, which) -> .setNeutralButton(R.string.create_shortcut, (dialog, which) ->
ShortcutUtil.requestPinLaunchShortcut()) ShortcutUtil.requestPinLaunchShortcut(null))
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
.create(); .create();
} }

View File

@ -157,16 +157,6 @@ public class SettingsFragment extends BaseFragment {
}); });
} }
Preference shortcut = findPreference("add_shortcut");
if (shortcut != null) {
shortcut.setVisible(App.isParasitic());
shortcut.setEnabled(!ShortcutUtil.isLaunchShortcutPinned());
shortcut.setOnPreferenceClickListener(preference -> {
ShortcutUtil.requestPinLaunchShortcut();
return true;
});
}
MaterialSwitchPreference notification = findPreference("enable_status_notification"); MaterialSwitchPreference notification = findPreference("enable_status_notification");
if (notification != null) { if (notification != null) {
if (App.isParasitic() && !ShortcutUtil.isLaunchShortcutPinned()) { if (App.isParasitic() && !ShortcutUtil.isLaunchShortcutPinned()) {
@ -182,6 +172,22 @@ public class SettingsFragment extends BaseFragment {
); );
} }
Preference shortcut = findPreference("add_shortcut");
if (shortcut != null) {
shortcut.setVisible(App.isParasitic());
shortcut.setEnabled(!ShortcutUtil.isLaunchShortcutPinned());
shortcut.setOnPreferenceClickListener(preference -> {
ShortcutUtil.requestPinLaunchShortcut(() -> {
shortcut.setEnabled(false);
if (notification != null) {
notification.setEnabled(true);
notification.setSummaryOn(R.string.settings_enable_status_notification_summary);
}
});
return true;
});
}
Preference backup = findPreference("backup"); Preference backup = findPreference("backup");
if (backup != null) { if (backup != null) {
backup.setEnabled(installed); backup.setEnabled(installed);

View File

@ -1,8 +1,13 @@
package org.lsposed.manager.util; package org.lsposed.manager.util;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
@ -17,6 +22,8 @@ import android.graphics.drawable.LayerDrawable;
import org.lsposed.manager.App; import org.lsposed.manager.App;
import org.lsposed.manager.R; import org.lsposed.manager.R;
import java.util.UUID;
public class ShortcutUtil { public class ShortcutUtil {
private static final String SHORTCUT_ID = "org.lsposed.manager.shortcut"; private static final String SHORTCUT_ID = "org.lsposed.manager.shortcut";
@ -71,7 +78,30 @@ public class ShortcutUtil {
return intent; return intent;
} }
public static void requestPinLaunchShortcut() { @SuppressLint("InlinedApi")
private static IntentSender registerReceiver(Context context, Runnable task) {
if (task == null) return null;
var uuid = UUID.randomUUID().toString();
var filter = new IntentFilter(uuid);
var permission = "android.permission.CREATE_USERS";
var receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
if (!uuid.equals(intent.getAction())) return;
context.unregisterReceiver(this);
task.run();
}
};
context.registerReceiver(receiver, filter, permission,
null/* main thread */, Context.RECEIVER_NOT_EXPORTED);
var intent = new Intent(uuid);
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
return PendingIntent.getBroadcast(context, 0, intent, flags).getIntentSender();
}
public static void requestPinLaunchShortcut(Runnable afterPinned) {
if (!App.isParasitic()) throw new RuntimeException(); if (!App.isParasitic()) throw new RuntimeException();
var context = App.getInstance(); var context = App.getInstance();
var info = new ShortcutInfo.Builder(context, SHORTCUT_ID) var info = new ShortcutInfo.Builder(context, SHORTCUT_ID)
@ -81,7 +111,7 @@ public class ShortcutUtil {
.setIcon(Icon.createWithAdaptiveBitmap(getBitmap(context, R.drawable.ic_launcher))) .setIcon(Icon.createWithAdaptiveBitmap(getBitmap(context, R.drawable.ic_launcher)))
.build(); .build();
var sm = context.getSystemService(ShortcutManager.class); var sm = context.getSystemService(ShortcutManager.class);
sm.requestPinShortcut(info, null); sm.requestPinShortcut(info, registerReceiver(context, afterPinned));
} }
public static boolean isLaunchShortcutPinned() { public static boolean isLaunchShortcutPinned() {