// sets the preferred launcher; if svmp is true, will set to LauncherActivity, otherwise will set // to aosp launcher protected static void setDefaultLauncher( Context context, PackageManager packageManager, boolean svmp) { Log.d(TAG, "Setting default launcher to: " + (svmp ? "svmp" : "aosp")); // set up args IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN); filter.addCategory(Intent.CATEGORY_HOME); filter.addCategory(Intent.CATEGORY_DEFAULT); int bestMatch = IntentFilter.MATCH_CATEGORY_EMPTY; ComponentName aospComponent = new ComponentName("com.cyanogenmod.trebuchet", "com.android.launcher3.Launcher"); ComponentName svmpComponent = new ComponentName(context.getPackageName(), LauncherActivity.class.getName()); ComponentName[] components = new ComponentName[] {aospComponent, svmpComponent}; // set preferred launcher and clear preferences for other launcher ComponentName preferred = svmp ? svmpComponent : aospComponent, other = svmp ? aospComponent : svmpComponent; packageManager.clearPackagePreferredActivities(other.getPackageName()); packageManager.addPreferredActivity(filter, bestMatch, components, preferred); if (svmp) { // start the preferred launcher Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.setComponent(preferred); launchIntent.addCategory(Intent.CATEGORY_HOME); launchIntent.addFlags( Intent .FLAG_ACTIVITY_NEW_TASK); // required to start an activity from outside of an Activity // context launchIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK); // when this is started, clear other launcher activities context.startActivity(launchIntent); } }
@Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); // check if the called back fragment is the one that has initiated the menu action // based on the group id. if not do noting switch (item.getItemId()) { case CONTEXT_TOGGLE_NAV_ID: LinearLayout resultLayout = (LinearLayout) findViewById(R.id.linearLayoutNavBar); if (resultLayout.isShown()) { resultLayout.setVisibility(View.GONE); } else { resultLayout.setVisibility(View.VISIBLE); } return true; case CONTEXT_PREFERENCES_ID: Intent intentPrefs = new Intent(this, PreferencesActivity.class); this.startActivity(intentPrefs); return true; case CONTEXT_README_ID: Intent intentReadMe = new Intent(this, ReadmeActivity.class); this.startActivity(intentReadMe); return true; case CONTEXT_CLEAR_ID: PackageManager localPackageManager = getPackageManager(); localPackageManager.clearPackagePreferredActivities("com.asksven.captivebrowser"); default: return false; } }
protected static void setDefaultLauncherWithoutStart( Context context, PackageManager packageManager, boolean svmp) { Log.d(TAG, "Setting default launcher to: " + (svmp ? "svmp" : "aosp")); // set up args IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN); filter.addCategory(Intent.CATEGORY_HOME); filter.addCategory(Intent.CATEGORY_DEFAULT); int bestMatch = IntentFilter.MATCH_CATEGORY_EMPTY; ComponentName aospComponent = new ComponentName("com.cyanogenmod.trebuchet", "com.android.launcher3.Launcher"); ComponentName svmpComponent = new ComponentName(context.getPackageName(), LauncherActivity.class.getName()); ComponentName[] components = new ComponentName[] {aospComponent, svmpComponent}; // set preferred launcher and clear preferences for other launcher ComponentName preferred = svmp ? svmpComponent : aospComponent, other = svmp ? aospComponent : svmpComponent; packageManager.clearPackagePreferredActivities(other.getPackageName()); packageManager.addPreferredActivity(filter, bestMatch, components, preferred); }