Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default application on AOSP

Question

Can I can set a default app on a build if two apps of the same category are installed?

Example

I am adding a custom browser on AOSP. I want to set it as the default browser before the build starts.

On the Android.mk file for packages there is an option to specify 'LOCAL_OVERRIDES_PACKAGES' which basically overrides the install of the packages mentioned making my app as the default.

But I would want the other app to be a part of the ROM with my app as the default.

Any ideas will be appreciated.

like image 523
xvf Avatar asked Dec 03 '15 18:12

xvf


People also ask

How do I make the app a default app on Android?

Find and tap Settings → Apps & notifications → Default apps. Tap the type of app you want to set, then tap the app you want to use as default.

What is set as default phone app?

Open the Settings apps. Tap Apps & Notifications. Tap Advanced. Tap Default Apps.


1 Answers

So I found a solution for setting an application as default at build time. I am documenting it in the hope that it helps someone else down the line.

  1. The Android system keeps a list of the default applications/activities in a block in a file located at /data/system/users/{*user-id*}/package-restrictions.xml called as <preferred-activities></preferred-activities>

  2. This file is generated by the Settings.java and the PackageManager.java upon build time. Whenever the defaults on the android system change, the flags on this xml block change accordingly.

  3. At build, the system reads a set of preferred-activities from an additional location which is /system/etc/preferred-activities/*.xml
  4. In order to add our desired preferred/default activities we create an xml file and place it at /system/etc/preferred-activities/ which is then read by the android system and the list of preferred activities are added to the list in package-restrictions.xml

Example

Adding a custom browser to AOSP

  • If only the default browser is being installed on the device other that 'mybrowser' the following xml file should be created. In this case, I am naming it as preferred-activies-home.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <preferred-activities>
          <item name="com.mybrowser.MainActivity" match="200000" always="true" set="2">
             <set name="com.mybrowser./.MainActivity" />
             <set name="com.android.browser/.BrowserActivity" />
             <filter>
                <action name="android.intent.action.VIEW" />
                <cat name="android.intent.category.DEFAULT" />
                <scheme name="http" />
             </filter>
          </item>
       </preferred-activities>
    
  • Copy the above xml to the /system/etc/preferred-apps/ location by adding the following line in <!--AOSP_SOURCE-->/build/target/product/full_base.mk

    PRODUCT_COPY_FILES +=/<location-of-file>/preferred-activities-home.xml:system/etc/preferred-apps/preferred-activities-home.xml
    
  • After build, the browser activity will be set as default

Limitations and Caveats

There are some limitations to the above mentioned process. They are as follows:

  • Upon build time, we would need to know the main activity for ALL the applications which are addressing the particular intent-filters.
  • The above process does not work for Launcher upon the first boot. The presumption is that the ResolverActivity does not consider the package-restrictions upon the startup.
like image 76
xvf Avatar answered Oct 02 '22 14:10

xvf