Home | History | Annotate | Download | only in defaultapps
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.applications.defaultapps;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.pm.ComponentInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.ResolveInfo;
     24 import android.graphics.drawable.Drawable;
     25 import android.net.Uri;
     26 import android.support.v7.preference.Preference;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 
     30 import java.util.List;
     31 
     32 public class DefaultBrowserPreferenceController extends DefaultAppPreferenceController {
     33 
     34     private static final String TAG = "BrowserPrefCtrl";
     35 
     36     static final Intent BROWSE_PROBE = new Intent()
     37             .setAction(Intent.ACTION_VIEW)
     38             .addCategory(Intent.CATEGORY_BROWSABLE)
     39             .setData(Uri.parse("http:"));
     40 
     41     public DefaultBrowserPreferenceController(Context context) {
     42         super(context);
     43     }
     44 
     45     @Override
     46     public boolean isAvailable() {
     47         final List<ResolveInfo> candidates = getCandidates();
     48         return candidates != null && !candidates.isEmpty();
     49     }
     50 
     51     @Override
     52     public String getPreferenceKey() {
     53         return "default_browser";
     54     }
     55 
     56     @Override
     57     public void updateState(Preference preference) {
     58         super.updateState(preference);
     59         final CharSequence defaultAppLabel = getDefaultAppLabel();
     60         if (!TextUtils.isEmpty(defaultAppLabel)) {
     61             preference.setSummary(defaultAppLabel);
     62         }
     63     }
     64 
     65     @Override
     66     protected DefaultAppInfo getDefaultAppInfo() {
     67         try {
     68             final String packageName = mPackageManager.getDefaultBrowserPackageNameAsUser(mUserId);
     69             Log.d(TAG, "Get default browser package: " + packageName);
     70             return new DefaultAppInfo(mPackageManager,
     71                     mPackageManager.getPackageManager().getApplicationInfo(packageName, 0));
     72         } catch (PackageManager.NameNotFoundException e) {
     73             return null;
     74         }
     75     }
     76 
     77     @Override
     78     public CharSequence getDefaultAppLabel() {
     79         if (!isAvailable()) {
     80             return null;
     81         }
     82         final DefaultAppInfo defaultApp = getDefaultAppInfo();
     83         final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null;
     84         if (!TextUtils.isEmpty(defaultAppLabel)) {
     85             return defaultAppLabel;
     86         }
     87         return getOnlyAppLabel();
     88     }
     89 
     90     @Override
     91     public Drawable getDefaultAppIcon() {
     92         if (!isAvailable()) {
     93             return null;
     94         }
     95         final DefaultAppInfo defaultApp = getDefaultAppInfo();
     96         if (defaultApp != null) {
     97             return defaultApp.loadIcon();
     98         }
     99         return getOnlyAppIcon();
    100     }
    101 
    102     private List<ResolveInfo> getCandidates() {
    103         return mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, PackageManager.MATCH_ALL,
    104                 mUserId);
    105     }
    106 
    107     private String getOnlyAppLabel() {
    108         // Resolve that intent and check that the handleAllWebDataURI boolean is set
    109         final List<ResolveInfo> list = getCandidates();
    110         if (list != null && list.size() == 1) {
    111             final ResolveInfo info = list.get(0);
    112             final String label = info.loadLabel(mPackageManager.getPackageManager()).toString();
    113             final ComponentInfo cn = info.getComponentInfo();
    114             final String packageName = cn == null ? null : cn.packageName;
    115             Log.d(TAG, "Getting label for the only browser app: " + packageName + label);
    116             return label;
    117         }
    118         return null;
    119     }
    120 
    121     private Drawable getOnlyAppIcon() {
    122         final List<ResolveInfo> list = getCandidates();
    123         if (list != null && list.size() == 1) {
    124             final ResolveInfo info = list.get(0);
    125             final ComponentInfo cn = info.getComponentInfo();
    126             final String packageName = cn == null ? null : cn.packageName;
    127             Log.d(TAG, "Getting icon for the only browser app: " + packageName);
    128             return info.loadIcon(mPackageManager.getPackageManager());
    129         }
    130         return null;
    131     }
    132 
    133     /**
    134      * Whether or not the pkg contains browser capability
    135      */
    136     public static boolean hasBrowserPreference(String pkg, Context context) {
    137         final Intent intent = new Intent(BROWSE_PROBE);
    138         intent.setPackage(pkg);
    139         final List<ResolveInfo> resolveInfos =
    140                 context.getPackageManager().queryIntentActivities(intent, 0);
    141         return resolveInfos != null && resolveInfos.size() != 0;
    142     }
    143 
    144     /**
    145      * Whether or not the pkg is the default browser
    146      */
    147     public boolean isBrowserDefault(String pkg, int userId) {
    148         String defaultPackage = mPackageManager.getDefaultBrowserPackageNameAsUser(userId);
    149         if (defaultPackage != null) {
    150             return defaultPackage.equals(pkg);
    151         }
    152 
    153         final List<ResolveInfo> list = mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE,
    154                 PackageManager.MATCH_ALL, userId);
    155         // There is only 1 app, it must be the default browser.
    156         return list != null && list.size() == 1;
    157     }
    158 }
    159