Android: Change Default Home Application

Android: change default Home Application

I did an extensive research on that and starting from 2.2 there is no way to do that. The only way is using some hacking that toddler lock app does but this app put samsung phones recently in the infinite loop, so it is a risky approach.

if you look at the froyo source code here of packagemanager class, you will see this small condition in the addPreferredActivity method:

if (getUidTargetSdkVersionLockedLP(Binder.getCallingUid())
< Build.VERSION_CODES.FROYO) {
Slog.w(TAG, "Ignoring addPreferredActivity() from uid"
+ Binder.getCallingUid());
return;
}

HomeSwitcher does not work properly on 2.2 since it uses this very method and developer made a comment on app page "Froyo(2.2) is not supported
due to the API change
"

Clearing and setting the default home application

The code to do this is actually just a very clever work around.

When a component with

        <category android:name="android.intent.category.HOME" />

is enabled, generally from an install of a new home application, the default home app gets cleared.

To take advantage of this by creating an empty activity with the home component like this.

<activity
android:name="com.t3hh4xx0r.haxlauncher.FakeHome"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

When you want to set your new default, you enable this component, then call the home intent
and then disable your fake home component again.

public static void makePrefered(Context c) {
PackageManager p = c.getPackageManager();
ComponentName cN = new ComponentName(c, FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
c.startActivity(selector);

p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

The end result is that the system thinks a new home app was installed, so the default is cleared allowing you to set yours with no special permissions.

Thank you to Kevin from TeslaCoil and NovaLauncher for the information on how this is done!

How to set default app launcher programmatically?

This is actually possible with a little workaround:

Create an empty Activity that acts as a launcher called FakeLauncherActivity. Add it to your manifest as a disabled component:

<activity
android:name="com.path.to.your.FakeLauncherActivity"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Check whether your desired launcher activity is the default one (with the isMyAppLauncherDefault() from your question).

If not, offer the user to choose the preferred launcher activity like this:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, com.path.to.your.FakeLauncherActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selector);

packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}

This method temporarily enables FakeLauncherActivity, which leads to a change in the set of available launcher activities, which forces Android to forget its default launcher. You will see something like...

521-735/system_process I/PackageManager﹕ Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 } type null

... in your log.

The method then simply opens a launcher intent where you can see all installed launchers and the buttons "Always" / "Just once".
Finally, the method disables FakeLauncherActivity again so that it doesn't display in the list.

You could repeat that as often as you want and only let the user proceed if your desired launcher activity is set as default.

Set and unset default app in Android

Did you try

getPackageManager().clearPackagePreferredActivities(getPackageName());

According to How to reset default launcher/home screen replacement? this resets the preferred launcher. Maybe it also works for the home button.

Alternatively, did you have a look at: Clearing and setting the default home application?

How to open Default Home App screen intent on Android

PackageManager packageManager = activity.getPackageManager();
ComponentName componentName = new ComponentName(activity, YourLauncher.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
activity.startActivity(intent);
activity.finishAndRemoveTask();

and launcher activity in the manifest:

        <activity
...
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>


Related Topics



Leave a reply



Submit