Clearing and Setting the Default Home Application

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!

Set default Home app dynamically

Can I set dynamically within an activity the preferred/default application that's to be launched when I push the Home key?

No. There is no exposed API for this.

If that's not possible, can I at least reset current defaults so that the chooser window re-appears?

Only if you are implementing the home screen that is the current default.

I tried to implement the latter by calling PackageManager.clearPackagePreferredActivities("com.android.launcher")

As the documentation for that method states, "An application can only clear its own package(s).".

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
"

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>

Is there a way to set a default home/launcher programatically on Android?

It is not possible to set a default HOME application from code, if you could do it it would be a security issue.

Android Setting New default home in code

Ok, I found the answer to my question.

The answer is here:
http://www.trustydroid.com/2014/05/19/force-show-default-app-chooser-dialog/

The one thing you should keep in mind is that the Component Name MUST be correct. Apparently under API14, it ignores the component (and acts like it works) if the class name is not correct. So it goes through the motions of Enabling the component, Launching the Chooser, then disabling the component. However, it never saves the new default.

After I tried compiling under API19, the OS threw an exception which lead me to fix the issue I was having with it working (which was an incorrect class name).

Once I had that straightened out, it did exactly what I wanted.

For completeness sake, here is the code:

Create a FakeActivity like this:

[Activity(Label = "FakeLauncher", Enabled = false)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryHome, Intent.CategoryDefault })]
public class FakeLauncher : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Create your application here
}
}

Then where ever you want to change your default home you run this code:

 ComponentName componentName = new ComponentName(Application.PackageName, "<fake activity class name>");
PackageManager.SetComponentEnabledSetting(componentName, Android.Content.PM.ComponentEnabledState.Enabled, Android.Content.PM.ComponentEnableOption.DontKillApp);

Intent tempIntent = new Intent(Intent.ActionMain);
tempIntent.AddCategory(Intent.CategoryHome);

StartActivity(tempIntent);

PackageManager.SetComponentEnabledSetting(componentName, Android.Content.PM.ComponentEnabledState.Disabled, Android.Content.PM.ComponentEnableOption.DontKillApp);

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?

Resetting default launcher to system factory default

The user always needs to confirm a change in the default settings. You can always trigger the "default launcher" dialog by

PackageManager pm = getPackageManager();
ComponentName cm = new ComponentName(this, FakeLauncherActivity.class);
pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

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

pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Where FakeLauncherActivity is just another Activity in your app with the intent-filter HOME/MAIN/DEFAULT (without LAUNCHER).

If you want to know the default system launcher you could query the intents answering the LAUNCHER Intent, but if the user has another Launcher installed, you will not know which one is the default system launcher.

Even if this is not a complete solution for your question, I hope it helps you in a useful direction.



Related Topics



Leave a reply



Submit