Unconditional allow create shortcut (#2458)

Except the default desktop is not supported
This commit is contained in:
Howard Wu 2023-04-01 18:15:04 +08:00 committed by GitHub
parent 08e72d32b9
commit 75965510f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 20 deletions

View File

@ -26,6 +26,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
@ -263,8 +264,9 @@ public class MainActivity extends BaseActivity implements RepoLoader.RepoListene
}
}
}
if(App.isParasitic){
ShortcutUtil.updateShortcut();
if (App.isParasitic) {
var updateShortcut = ShortcutUtil.updateShortcut();
Log.d(App.TAG, "update shortcut success = " + updateShortcut);
}
}

View File

@ -30,7 +30,7 @@ import androidx.fragment.app.FragmentManager;
import org.lsposed.manager.App;
import org.lsposed.manager.ConfigManager;
import org.lsposed.manager.R;
import org.lsposed.manager.ui.fragment.HomeFragment;
import org.lsposed.manager.ui.fragment.BaseFragment;
import org.lsposed.manager.util.ShortcutUtil;
public class WelcomeDialog extends DialogFragment {
@ -46,9 +46,13 @@ public class WelcomeDialog extends DialogFragment {
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply())
.setPositiveButton(android.R.string.ok, null)
.setNeutralButton(R.string.create_shortcut, (dialog, which) -> {
if (!ShortcutUtil.requestPinLaunchShortcut(() -> App.getPreferences().edit()
.putBoolean("never_show_welcome", true).apply())) {
var home = (HomeFragment) getParentFragment();
var home = (BaseFragment) getParentFragment();
if (!ShortcutUtil.requestPinLaunchShortcut(() -> {
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply();
if (home != null) {
home.showHint(R.string.settings_shortcut_pinned_hint, false);
}
})) {
if (home != null) {
home.showHint(R.string.settings_unsupported_pin_shortcut_summary, false);
}

View File

@ -177,14 +177,13 @@ public class SettingsFragment extends BaseFragment {
Preference shortcut = findPreference("add_shortcut");
if (shortcut != null) {
shortcut.setEnabled(ShortcutUtil.shouldAllowPinShortcut(requireContext()));
shortcut.setVisible(App.isParasitic);
if (ShortcutUtil.isLaunchShortcutPinned()) {
if (!ShortcutUtil.isRequestPinShortcutSupported(requireContext())) {
shortcut.setEnabled(false);
shortcut.setSummary(R.string.settings_unsupported_pin_shortcut_summary);
} else if (!ShortcutUtil.shouldAllowPinShortcut(requireContext()))
shortcut.setSummary(R.string.settings_created_shortcut_summary);
} else {
shortcut.setEnabled(true);
shortcut.setSummary(R.string.settings_create_shortcut_summary);
}
shortcut.setOnPreferenceClickListener(preference -> {
if (!ShortcutUtil.requestPinLaunchShortcut(() -> {
shortcut.setEnabled(false);
@ -194,6 +193,7 @@ public class SettingsFragment extends BaseFragment {
notification.setSummaryOn(R.string.settings_enable_status_notification_summary);
}
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply();
parentFragment.showHint(R.string.settings_shortcut_pinned_hint, false);
})) {
parentFragment.showHint(R.string.settings_unsupported_pin_shortcut_summary, true);
}

View File

@ -38,7 +38,8 @@ import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.util.Log;
import androidx.annotation.Nullable;
import org.lsposed.manager.App;
import org.lsposed.manager.R;
@ -49,6 +50,8 @@ import java.util.UUID;
public class ShortcutUtil {
private static final String SHORTCUT_ID = "org.lsposed.manager.shortcut";
private static boolean shortcutPinned = false;
private static String defaultLauncherPackageName = null;
private static Bitmap getBitmap(Context context, int id) {
var r = context.getResources();
@ -114,17 +117,13 @@ public class ShortcutUtil {
if (!uuid.equals(intent.getAction())) return;
context.unregisterReceiver(this);
task.run();
defaultLauncherPackageName = getDefaultLauncherPackageName(context);
shortcutPinned = true;
}
};
context.registerReceiver(receiver, filter, permission,
null/* main thread */, Context.RECEIVER_NOT_EXPORTED);
App.getMainHandler().postDelayed(() -> {
if (isLaunchShortcutPinned()) {
task.run();
}
}, 1000);
var intent = new Intent(uuid);
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
return PendingIntent.getBroadcast(context, 0, intent, flags).getIntentSender();
@ -144,7 +143,7 @@ public class ShortcutUtil {
return builder;
}
public static boolean isRequestPinShortcutSupported(Context context) {
public static boolean isRequestPinShortcutSupported(Context context) throws RuntimeException {
var sm = context.getSystemService(ShortcutManager.class);
return sm.isRequestPinShortcutSupported();
}
@ -160,7 +159,6 @@ public class ShortcutUtil {
public static boolean updateShortcut() {
if (!isLaunchShortcutPinned()) return false;
Log.d(App.TAG, "update shortcut");
var context = App.getInstance();
var sm = context.getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
@ -180,4 +178,22 @@ public class ShortcutUtil {
}
return pinned;
}
public static boolean shouldAllowPinShortcut(Context context) {
if (shortcutPinned)
if (defaultLauncherPackageName == null
|| !defaultLauncherPackageName.equals(getDefaultLauncherPackageName(context)))
shortcutPinned = false;
defaultLauncherPackageName = getDefaultLauncherPackageName(context);
if (!isLaunchShortcutPinned()) return true;
return !shortcutPinned;
}
@Nullable
private static String getDefaultLauncherPackageName(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
var resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
return resolveInfo.activityInfo.packageName;
}
}

View File

@ -199,6 +199,7 @@
<string name="settings_translation">Participate in translation</string>
<string name="settings_translation_summary">Help us translate %s into your language</string>
<string name="settings_create_shortcut_summary">Create a shortcut that can open parasitic manager</string>
<string name="settings_shortcut_pinned_hint">Shortcut pinned</string>
<string name="settings_created_shortcut_summary">A shortcut has been created to open parasitic manager</string>
<string name="settings_unsupported_pin_shortcut_summary">The current default launcher does not support pin shortcuts</string>
<string name="settings_enable_status_notification">Status Notification</string>