6

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.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
xvf
  • 330
  • 3
  • 13
  • What do you mean 'of the same category'? – CJBS Dec 04 '15 at 17:07
  • Possible duplicate (see this answer): http://stackoverflow.com/questions/11312576/how-would-i-make-an-embedded-android-os-with-just-one-app/11332180#11332180 – CJBS Dec 04 '15 at 17:09
  • Also see: http://stackoverflow.com/questions/22911156/android-open-source-setting-the-default-launcher?lq=1 – CJBS Dec 04 '15 at 17:17
  • 1
    @CJBS By 'of the same category' I mean two apps with intent-filter with category Home or Browsable. Both the referenced links point to **overriding** an installation- prevent the installation of the other app. I want my app to co-exist with the existing app and be set as default. – xvf Dec 07 '15 at 16:06
  • 2
    Indeed, neither of CJBS's links, nor their answer, address the specific question which is being asked here. I'm rolling back their edits, which try to change the focus of the question from what was asked, towards a *different* question which they apparently feel like answering. This question is *not* about kiosks, *not* about locking out alternatives, and *not* about the launcher, as there are many sources of Intents and for the mentioned browser functionality the launcher isn't even one of the primary ones. – Chris Stratton Dec 09 '15 at 06:57
  • @ChrisStratton You are correct. I have removed my answer, and your rollback of the term 'kiosk' is also warranted. – CJBS Dec 09 '15 at 17:16
  • In terms of the problem itself, unless you can think of an example that comes pre-configured as a default which you could learn from, I'd probably approach it by looking at the code within the Intent resolution mechanism which handles defaults. See if there is any interface for setting one apart from the user choose popup, and if not add your own mechanism or hardcoding. Also make sure to think about interaction with whatever clears defaults - it seems like you want the user to be able to override your default, but you also want it to be a default in the case of a factory reset, etc. – Chris Stratton Dec 10 '15 at 02:11

1 Answers1

11

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.
xvf
  • 330
  • 3
  • 13
  • I would be really grateful if you could elaborate more on the Limitaion[1] ? – Ronak Khandelwal Aug 07 '17 at 04:03
  • 1
    If we review the code block above, you can see that while specifying browser MainActivity, we listed out the different activities which will be used as default. The limitation is that you would have to know the main activity of all the applications to be used by the intent-filter, including the package name. Hope this helps. – xvf Aug 08 '17 at 18:32
  • 1
    Does this apply for Android Pie? – ilbets Mar 05 '19 at 15:49
  • @CVS `package-restrictions.xml` seems to have limited number of preferred activities on Pie. So is the case with `dumpsys package preferred-xml` and `dumpsys package pref`. May be it's shifted to some other database file or it's handled on the fly by `system_server` as `getPreferredActivities` and `getPreferredPackages` are being deprecated in Q. – Irfan Latif Nov 13 '19 at 17:39
  • Does the .xml file go in /system/etc/preferred-apps/ or /system/etc/preferred-activities/? You mention both. – TheGwa May 28 '20 at 19:16