Search in sources :

Example 36 with ShortcutManager

use of android.content.pm.ShortcutManager in project rebootmenu by ryuunoakaihitomi.

the class Shortcut method onCreate.

// 来自UnRootMode.java -- 结束
@TargetApi(Build.VERSION_CODES.N_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    componentName = new ComponentName(this, AdminReceiver.class);
    devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
    // shortcut功能选项
    final int REBOOT = 2;
    final int SHUTDOWN = 3;
    final int RECOVERY = 8;
    final int FASTBOOT = 9;
    final int REBOOT_UI = 4;
    final int UR_REBOOT = 10;
    final int LOCKSCREEN = 5;
    final int UR_LOCKSCREEN = 6;
    final int UR_POWERDIALOG = 7;
    int param = getIntent().getIntExtra(extraTag, -1);
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    switch(param) {
        // Root模式下的快捷方式
        case ROOT:
            ShortcutInfo fastboot = new ShortcutInfo.Builder(this, "r_ffb").setShortLabel("*" + getString(R.string.fastboot_short)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_sort_by_size)).setIntent(new Intent(action).putExtra(extraTag, FASTBOOT)).setRank(5).build();
            ShortcutInfo recovery = new ShortcutInfo.Builder(this, "r_frr").setShortLabel("*" + getString(R.string.recovery_short)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_today)).setIntent(new Intent(action).putExtra(extraTag, RECOVERY)).setRank(4).build();
            ShortcutInfo reboot = new ShortcutInfo.Builder(this, "r_fr").setShortLabel("*" + getString(R.string.reboot)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_rotate)).setIntent(new Intent(action).putExtra(extraTag, REBOOT)).setRank(3).build();
            ShortcutInfo shutdown = new ShortcutInfo.Builder(this, "r_fs").setShortLabel("*" + getString(R.string.shutdown)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_delete)).setIntent(new Intent(action).putExtra(extraTag, SHUTDOWN)).setRank(2).build();
            ShortcutInfo rebootui = new ShortcutInfo.Builder(this, "r_ru").setShortLabel(getString(R.string.reboot_ui)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_view)).setIntent(new Intent(action).putExtra(extraTag, REBOOT_UI)).setRank(1).build();
            ShortcutInfo lockscreen = new ShortcutInfo.Builder(this, "r_l").setShortLabel(getString(R.string.lockscreen)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_slideshow)).setIntent(new Intent(action).putExtra(extraTag, LOCKSCREEN)).setRank(0).build();
            assert shortcutManager != null;
            // 无论如何,没有root不能用shell锁屏和重启UI,所以在不检查root模式下的无root模式改为增加fast boot。
            if (ShellUtils.isRoot())
                shortcutManager.setDynamicShortcuts(Arrays.asList(recovery, reboot, shutdown, rebootui, lockscreen));
            else
                shortcutManager.setDynamicShortcuts(Arrays.asList(fastboot, recovery, reboot, shutdown));
            finish();
            break;
        // 免root模式下的快捷方式
        case UNROOT:
            boolean isDevOwner = devicePolicyManager.isDeviceOwnerApp(getPackageName());
            ShortcutInfo ur_reboot = null;
            if (isDevOwner)
                ur_reboot = new ShortcutInfo.Builder(this, "ur_r").setShortLabel(getString(R.string.reboot)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_rotate)).setIntent(new Intent(action).putExtra(extraTag, UR_REBOOT)).setRank(2).build();
            ShortcutInfo ur_lockscreen = new ShortcutInfo.Builder(this, "ur_l").setShortLabel(getString(R.string.lockscreen)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_slideshow)).setIntent(new Intent(action).putExtra(extraTag, UR_LOCKSCREEN)).setRank(1).build();
            ShortcutInfo ur_powerdialog = new ShortcutInfo.Builder(this, "ur_p").setShortLabel(getString(R.string.system_power_dialog)).setIcon(Icon.createWithResource(this, android.R.drawable.ic_menu_preferences)).setIntent(new Intent(action).putExtra(extraTag, UR_POWERDIALOG)).setRank(0).build();
            assert shortcutManager != null;
            if (isDevOwner)
                shortcutManager.setDynamicShortcuts(Arrays.asList(ur_lockscreen, ur_powerdialog, ur_reboot));
            else
                shortcutManager.setDynamicShortcuts(Arrays.asList(ur_lockscreen, ur_powerdialog));
            finish();
            break;
        // 快捷方式使用强制执行
        case FASTBOOT:
            rebootExec("bootloader");
            break;
        case RECOVERY:
            rebootExec("recovery");
            break;
        case REBOOT:
            rebootExec(null);
            break;
        case SHUTDOWN:
            rebootExec("-p");
            break;
        case REBOOT_UI:
            ShellUtils.suCmdExec("busybox pkill com.android.systemui");
            finish();
            break;
        case LOCKSCREEN:
            ShellUtils.suCmdExec("input keyevent 26");
            finish();
            break;
        // 免root模式
        case UR_LOCKSCREEN:
            lockscreen();
            break;
        case UR_POWERDIALOG:
            UnRootMode.accessbilityon(Shortcut.this);
            break;
        case UR_REBOOT:
            if (devicePolicyManager.isDeviceOwnerApp(getPackageName()))
                UnRootMode.reboot(devicePolicyManager, componentName, this);
            else {
                int random = new Random().nextInt(99);
                // 保留日志
                new DebugLog("DEVICE_OWNER_DISABLED from SHORTCUT random=" + random, DebugLog.LogLevel.V);
                new TextToast(getApplicationContext(), random > 50, getString(R.string.device_owner_disabled));
                finish();
            }
        default:
            finish();
    }
}
Also used : DebugLog(com.ryuunoakaihitomi.rebootmenu.util.DebugLog) ShortcutInfo(android.content.pm.ShortcutInfo) Random(java.util.Random) ShortcutManager(android.content.pm.ShortcutManager) TextToast(com.ryuunoakaihitomi.rebootmenu.util.TextToast) ComponentName(android.content.ComponentName) Intent(android.content.Intent) TargetApi(android.annotation.TargetApi)

Example 37 with ShortcutManager

use of android.content.pm.ShortcutManager in project VirtualXposed by android-hacker.

the class VirtualCore method createShortcutAboveN.

@TargetApi(Build.VERSION_CODES.N_MR1)
private static boolean createShortcutAboveN(Context context, ShortcutInfo likeShortcut) {
    ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
    if (shortcutManager == null) {
        return false;
    }
    try {
        int max = shortcutManager.getMaxShortcutCountPerActivity();
        List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
        if (dynamicShortcuts.size() >= max) {
            Collections.sort(dynamicShortcuts, new Comparator<ShortcutInfo>() {

                @Override
                public int compare(ShortcutInfo o1, ShortcutInfo o2) {
                    long r = o1.getLastChangedTimestamp() - o2.getLastChangedTimestamp();
                    return r == 0 ? 0 : (r > 0 ? 1 : -1);
                }
            });
            // remove old.
            ShortcutInfo remove = dynamicShortcuts.remove(0);
            shortcutManager.removeDynamicShortcuts(Collections.singletonList(remove.getId()));
        }
        shortcutManager.addDynamicShortcuts(Collections.singletonList(likeShortcut));
        return true;
    } catch (Throwable e) {
        return false;
    }
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) ShortcutManager(android.content.pm.ShortcutManager) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Example 38 with ShortcutManager

use of android.content.pm.ShortcutManager in project android_packages_apps_Settings by omnirom.

the class CreateShortcut method createResultIntent.

protected Intent createResultIntent(Intent shortcutIntent, ResolveInfo resolveInfo, CharSequence label) {
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    ShortcutManager sm = getSystemService(ShortcutManager.class);
    ActivityInfo activityInfo = resolveInfo.activityInfo;
    Icon maskableIcon = activityInfo.icon != 0 ? Icon.createWithAdaptiveBitmap(createIcon(activityInfo.icon, R.layout.shortcut_badge_maskable, getResources().getDimensionPixelSize(R.dimen.shortcut_size_maskable))) : Icon.createWithResource(this, R.drawable.ic_launcher_settings);
    String shortcutId = SHORTCUT_ID_PREFIX + shortcutIntent.getComponent().flattenToShortString();
    ShortcutInfo info = new ShortcutInfo.Builder(this, shortcutId).setShortLabel(label).setIntent(shortcutIntent).setIcon(maskableIcon).build();
    Intent intent = sm.createShortcutResultIntent(info);
    if (intent == null) {
        intent = new Intent();
    }
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_settings));
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
    if (activityInfo.icon != 0) {
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(activityInfo.icon, R.layout.shortcut_badge, getResources().getDimensionPixelSize(R.dimen.shortcut_size)));
    }
    return intent;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) ShortcutInfo(android.content.pm.ShortcutInfo) ShortcutManager(android.content.pm.ShortcutManager) Intent(android.content.Intent) Icon(android.graphics.drawable.Icon)

Example 39 with ShortcutManager

use of android.content.pm.ShortcutManager in project Taskbar by farmerbb.

the class MainActivity method proceedWithAppLaunch.

private void proceedWithAppLaunch(Bundle savedInstanceState) {
    setContentView(R.layout.main);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setCustomView(R.layout.switch_layout);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
    }
    theSwitch = U.findViewById(this, R.id.the_switch);
    if (theSwitch != null) {
        final SharedPreferences pref = U.getSharedPreferences(this);
        theSwitch.setChecked(pref.getBoolean("taskbar_active", false));
        theSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                if (U.canDrawOverlays(this)) {
                    boolean firstRun = pref.getBoolean("first_run", true);
                    startTaskbarService();
                    if (firstRun)
                        U.showRecentAppsDialog(this);
                } else {
                    U.showPermissionDialog(this);
                    compoundButton.setChecked(false);
                }
            } else
                stopTaskbarService();
        });
    }
    if (savedInstanceState == null) {
        if (!getIntent().hasExtra("theme_change"))
            getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new AboutFragment(), "AboutFragment").commit();
        else
            getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new AppearanceFragment(), "AppearanceFragment").commit();
    }
    if (!BuildConfig.APPLICATION_ID.equals(BuildConfig.BASE_APPLICATION_ID) && freeVersionInstalled()) {
        final SharedPreferences pref = U.getSharedPreferences(this);
        if (!pref.getBoolean("dont_show_uninstall_dialog", false)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.settings_imported_successfully).setMessage(R.string.import_dialog_message).setPositiveButton(R.string.action_uninstall, (dialog, which) -> {
                pref.edit().putBoolean("uninstall_dialog_shown", true).apply();
                try {
                    startActivity(new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + BuildConfig.BASE_APPLICATION_ID)));
                } catch (ActivityNotFoundException e) {
                /* Gracefully fail */
                }
            });
            if (pref.getBoolean("uninstall_dialog_shown", false))
                builder.setNegativeButton(R.string.action_dont_show_again, (dialogInterface, i) -> pref.edit().putBoolean("dont_show_uninstall_dialog", true).apply());
            AlertDialog dialog = builder.create();
            dialog.show();
            dialog.setCancelable(false);
        }
        if (!pref.getBoolean("uninstall_dialog_shown", false)) {
            if (theSwitch != null)
                theSwitch.setChecked(false);
            SharedPreferences.Editor editor = pref.edit();
            String iconPack = pref.getString("icon_pack", BuildConfig.BASE_APPLICATION_ID);
            if (iconPack.contains(BuildConfig.BASE_APPLICATION_ID)) {
                editor.putString("icon_pack", BuildConfig.APPLICATION_ID);
            } else {
                U.refreshPinnedIcons(this);
            }
            editor.putBoolean("first_run", true);
            editor.apply();
        }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
        if (shortcutManager.getDynamicShortcuts().size() == 0) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClass(this, StartTaskbarActivity.class);
            intent.putExtra("is_launching_shortcut", true);
            ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "start_taskbar").setShortLabel(getString(R.string.start_taskbar)).setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_start)).setIntent(intent).build();
            Intent intent2 = new Intent(Intent.ACTION_MAIN);
            intent2.setClass(this, ShortcutActivity.class);
            intent2.putExtra("is_launching_shortcut", true);
            ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "freeform_mode").setShortLabel(getString(R.string.pref_header_freeform)).setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_freeform)).setIntent(intent2).build();
            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2));
        }
    }
}
Also used : AlertDialog(android.app.AlertDialog) Context(android.content.Context) Arrays(java.util.Arrays) Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) StartMenuService(com.farmerbb.taskbar.service.StartMenuService) Uri(android.net.Uri) Intent(android.content.Intent) FragmentTransaction(android.app.FragmentTransaction) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) NotificationService(com.farmerbb.taskbar.service.NotificationService) StartTaskbarActivity(com.farmerbb.taskbar.activity.StartTaskbarActivity) PackageInfo(android.content.pm.PackageInfo) IconCache(com.farmerbb.taskbar.util.IconCache) AboutFragment(com.farmerbb.taskbar.fragment.AboutFragment) Icon(android.graphics.drawable.Icon) Build(android.os.Build) ActionBar(android.support.v7.app.ActionBar) LauncherHelper(com.farmerbb.taskbar.util.LauncherHelper) ComponentName(android.content.ComponentName) ShortcutInfo(android.content.pm.ShortcutInfo) IntentFilter(android.content.IntentFilter) U(com.farmerbb.taskbar.util.U) SwitchCompat(android.support.v7.widget.SwitchCompat) AppearanceFragment(com.farmerbb.taskbar.fragment.AppearanceFragment) BroadcastReceiver(android.content.BroadcastReceiver) ShortcutManager(android.content.pm.ShortcutManager) AppCompatActivity(android.support.v7.app.AppCompatActivity) FreeformHackHelper(com.farmerbb.taskbar.util.FreeformHackHelper) File(java.io.File) AlertDialog(android.app.AlertDialog) TaskbarService(com.farmerbb.taskbar.service.TaskbarService) SharedPreferences(android.content.SharedPreferences) DashboardService(com.farmerbb.taskbar.service.DashboardService) ActivityNotFoundException(android.content.ActivityNotFoundException) KeyboardShortcutActivity(com.farmerbb.taskbar.activity.KeyboardShortcutActivity) ShortcutActivity(com.farmerbb.taskbar.activity.ShortcutActivity) HomeActivity(com.farmerbb.taskbar.activity.HomeActivity) ImportSettingsActivity(com.farmerbb.taskbar.activity.ImportSettingsActivity) AboutFragment(com.farmerbb.taskbar.fragment.AboutFragment) ShortcutInfo(android.content.pm.ShortcutInfo) SharedPreferences(android.content.SharedPreferences) ShortcutManager(android.content.pm.ShortcutManager) Intent(android.content.Intent) AppearanceFragment(com.farmerbb.taskbar.fragment.AppearanceFragment) ActivityNotFoundException(android.content.ActivityNotFoundException) ActionBar(android.support.v7.app.ActionBar)

Example 40 with ShortcutManager

use of android.content.pm.ShortcutManager in project Taskbar by farmerbb.

the class CompatUtils method pinAppShortcut.

public static void pinAppShortcut(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
        if (mShortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, "freeform_mode").build();
            mShortcutManager.requestPinShortcut(pinShortcutInfo, null);
        } else
            U.showToastLong(context, R.string.pin_shortcut_not_supported);
    } else
        U.pinAppShortcut(context);
}
Also used : ShortcutInfo(android.content.pm.ShortcutInfo) ShortcutManager(android.content.pm.ShortcutManager)

Aggregations

ShortcutManager (android.content.pm.ShortcutManager)55 ShortcutInfo (android.content.pm.ShortcutInfo)39 Intent (android.content.Intent)31 ArrayList (java.util.ArrayList)19 TargetApi (android.annotation.TargetApi)15 PendingIntent (android.app.PendingIntent)7 Icon (android.graphics.drawable.Icon)7 ActivityInfo (android.content.pm.ActivityInfo)6 Bundle (android.os.Bundle)6 ComponentName (android.content.ComponentName)5 ResolveInfo (android.content.pm.ResolveInfo)4 PackageManager (android.content.pm.PackageManager)3 WorkerThread (android.support.annotation.WorkerThread)3 SuppressLint (android.annotation.SuppressLint)2 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Cursor (android.database.Cursor)2 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 RequiresApi (android.support.annotation.RequiresApi)2 VisibleForTesting (androidx.annotation.VisibleForTesting)2