Unconditional allow create shortcut (#2458)
Except the default desktop is not supported
This commit is contained in:
parent
08e72d32b9
commit
75965510f3
|
|
@ -26,6 +26,7 @@ import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
|
||||||
|
|
@ -264,7 +265,8 @@ public class MainActivity extends BaseActivity implements RepoLoader.RepoListene
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (App.isParasitic) {
|
if (App.isParasitic) {
|
||||||
ShortcutUtil.updateShortcut();
|
var updateShortcut = ShortcutUtil.updateShortcut();
|
||||||
|
Log.d(App.TAG, "update shortcut success = " + updateShortcut);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ import androidx.fragment.app.FragmentManager;
|
||||||
import org.lsposed.manager.App;
|
import org.lsposed.manager.App;
|
||||||
import org.lsposed.manager.ConfigManager;
|
import org.lsposed.manager.ConfigManager;
|
||||||
import org.lsposed.manager.R;
|
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;
|
import org.lsposed.manager.util.ShortcutUtil;
|
||||||
|
|
||||||
public class WelcomeDialog extends DialogFragment {
|
public class WelcomeDialog extends DialogFragment {
|
||||||
|
|
@ -46,9 +46,13 @@ public class WelcomeDialog extends DialogFragment {
|
||||||
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply())
|
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply())
|
||||||
.setPositiveButton(android.R.string.ok, null)
|
.setPositiveButton(android.R.string.ok, null)
|
||||||
.setNeutralButton(R.string.create_shortcut, (dialog, which) -> {
|
.setNeutralButton(R.string.create_shortcut, (dialog, which) -> {
|
||||||
if (!ShortcutUtil.requestPinLaunchShortcut(() -> App.getPreferences().edit()
|
var home = (BaseFragment) getParentFragment();
|
||||||
.putBoolean("never_show_welcome", true).apply())) {
|
if (!ShortcutUtil.requestPinLaunchShortcut(() -> {
|
||||||
var home = (HomeFragment) getParentFragment();
|
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply();
|
||||||
|
if (home != null) {
|
||||||
|
home.showHint(R.string.settings_shortcut_pinned_hint, false);
|
||||||
|
}
|
||||||
|
})) {
|
||||||
if (home != null) {
|
if (home != null) {
|
||||||
home.showHint(R.string.settings_unsupported_pin_shortcut_summary, false);
|
home.showHint(R.string.settings_unsupported_pin_shortcut_summary, false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -177,14 +177,13 @@ public class SettingsFragment extends BaseFragment {
|
||||||
|
|
||||||
Preference shortcut = findPreference("add_shortcut");
|
Preference shortcut = findPreference("add_shortcut");
|
||||||
if (shortcut != null) {
|
if (shortcut != null) {
|
||||||
|
shortcut.setEnabled(ShortcutUtil.shouldAllowPinShortcut(requireContext()));
|
||||||
shortcut.setVisible(App.isParasitic);
|
shortcut.setVisible(App.isParasitic);
|
||||||
if (ShortcutUtil.isLaunchShortcutPinned()) {
|
if (!ShortcutUtil.isRequestPinShortcutSupported(requireContext())) {
|
||||||
shortcut.setEnabled(false);
|
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);
|
shortcut.setSummary(R.string.settings_created_shortcut_summary);
|
||||||
} else {
|
|
||||||
shortcut.setEnabled(true);
|
|
||||||
shortcut.setSummary(R.string.settings_create_shortcut_summary);
|
|
||||||
}
|
|
||||||
shortcut.setOnPreferenceClickListener(preference -> {
|
shortcut.setOnPreferenceClickListener(preference -> {
|
||||||
if (!ShortcutUtil.requestPinLaunchShortcut(() -> {
|
if (!ShortcutUtil.requestPinLaunchShortcut(() -> {
|
||||||
shortcut.setEnabled(false);
|
shortcut.setEnabled(false);
|
||||||
|
|
@ -194,6 +193,7 @@ public class SettingsFragment extends BaseFragment {
|
||||||
notification.setSummaryOn(R.string.settings_enable_status_notification_summary);
|
notification.setSummaryOn(R.string.settings_enable_status_notification_summary);
|
||||||
}
|
}
|
||||||
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply();
|
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);
|
parentFragment.showHint(R.string.settings_unsupported_pin_shortcut_summary, true);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,8 @@ import android.graphics.drawable.Drawable;
|
||||||
import android.graphics.drawable.Icon;
|
import android.graphics.drawable.Icon;
|
||||||
import android.graphics.drawable.LayerDrawable;
|
import android.graphics.drawable.LayerDrawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.util.Log;
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import org.lsposed.manager.App;
|
import org.lsposed.manager.App;
|
||||||
import org.lsposed.manager.R;
|
import org.lsposed.manager.R;
|
||||||
|
|
@ -49,6 +50,8 @@ 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";
|
||||||
|
private static boolean shortcutPinned = false;
|
||||||
|
private static String defaultLauncherPackageName = null;
|
||||||
|
|
||||||
private static Bitmap getBitmap(Context context, int id) {
|
private static Bitmap getBitmap(Context context, int id) {
|
||||||
var r = context.getResources();
|
var r = context.getResources();
|
||||||
|
|
@ -114,17 +117,13 @@ public class ShortcutUtil {
|
||||||
if (!uuid.equals(intent.getAction())) return;
|
if (!uuid.equals(intent.getAction())) return;
|
||||||
context.unregisterReceiver(this);
|
context.unregisterReceiver(this);
|
||||||
task.run();
|
task.run();
|
||||||
|
defaultLauncherPackageName = getDefaultLauncherPackageName(context);
|
||||||
|
shortcutPinned = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
context.registerReceiver(receiver, filter, permission,
|
context.registerReceiver(receiver, filter, permission,
|
||||||
null/* main thread */, Context.RECEIVER_NOT_EXPORTED);
|
null/* main thread */, Context.RECEIVER_NOT_EXPORTED);
|
||||||
|
|
||||||
App.getMainHandler().postDelayed(() -> {
|
|
||||||
if (isLaunchShortcutPinned()) {
|
|
||||||
task.run();
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
var intent = new Intent(uuid);
|
var intent = new Intent(uuid);
|
||||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
|
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
|
||||||
return PendingIntent.getBroadcast(context, 0, intent, flags).getIntentSender();
|
return PendingIntent.getBroadcast(context, 0, intent, flags).getIntentSender();
|
||||||
|
|
@ -144,7 +143,7 @@ public class ShortcutUtil {
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isRequestPinShortcutSupported(Context context) {
|
public static boolean isRequestPinShortcutSupported(Context context) throws RuntimeException {
|
||||||
var sm = context.getSystemService(ShortcutManager.class);
|
var sm = context.getSystemService(ShortcutManager.class);
|
||||||
return sm.isRequestPinShortcutSupported();
|
return sm.isRequestPinShortcutSupported();
|
||||||
}
|
}
|
||||||
|
|
@ -160,7 +159,6 @@ public class ShortcutUtil {
|
||||||
|
|
||||||
public static boolean updateShortcut() {
|
public static boolean updateShortcut() {
|
||||||
if (!isLaunchShortcutPinned()) return false;
|
if (!isLaunchShortcutPinned()) return false;
|
||||||
Log.d(App.TAG, "update shortcut");
|
|
||||||
var context = App.getInstance();
|
var context = App.getInstance();
|
||||||
var sm = context.getSystemService(ShortcutManager.class);
|
var sm = context.getSystemService(ShortcutManager.class);
|
||||||
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
|
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
|
||||||
|
|
@ -180,4 +178,22 @@ public class ShortcutUtil {
|
||||||
}
|
}
|
||||||
return pinned;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -199,6 +199,7 @@
|
||||||
<string name="settings_translation">Participate in translation</string>
|
<string name="settings_translation">Participate in translation</string>
|
||||||
<string name="settings_translation_summary">Help us translate %s into your language</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_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_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_unsupported_pin_shortcut_summary">The current default launcher does not support pin shortcuts</string>
|
||||||
<string name="settings_enable_status_notification">Status Notification</string>
|
<string name="settings_enable_status_notification">Status Notification</string>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue