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.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.content.pm.ActivityInfo;
     24 import android.content.pm.PackageManager;
     25 import android.content.pm.ResolveInfo;
     26 
     27 import com.android.settings.R;
     28 import com.android.settingslib.applications.DefaultAppInfo;
     29 import com.android.settingslib.wrapper.PackageManagerWrapper;
     30 
     31 import java.util.ArrayList;
     32 import java.util.List;
     33 
     34 public class DefaultHomePreferenceController extends DefaultAppPreferenceController {
     35 
     36     static final IntentFilter HOME_FILTER;
     37 
     38     private final String mPackageName;
     39 
     40     static {
     41         HOME_FILTER = new IntentFilter(Intent.ACTION_MAIN);
     42         HOME_FILTER.addCategory(Intent.CATEGORY_HOME);
     43         HOME_FILTER.addCategory(Intent.CATEGORY_DEFAULT);
     44     }
     45 
     46     public DefaultHomePreferenceController(Context context) {
     47         super(context);
     48         mPackageName = mContext.getPackageName();
     49     }
     50 
     51     @Override
     52     public String getPreferenceKey() {
     53         return "default_home";
     54     }
     55 
     56     @Override
     57     public boolean isAvailable() {
     58         return mContext.getResources().getBoolean(R.bool.config_show_default_home);
     59     }
     60 
     61     @Override
     62     protected DefaultAppInfo getDefaultAppInfo() {
     63         final ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
     64         final ComponentName currentDefaultHome = mPackageManager.getHomeActivities(homeActivities);
     65         if (currentDefaultHome != null) {
     66             return new DefaultAppInfo(mContext, mPackageManager, mUserId, currentDefaultHome);
     67         }
     68         final ActivityInfo onlyAppInfo = getOnlyAppInfo(homeActivities);
     69         if (onlyAppInfo != null) {
     70             return new DefaultAppInfo(mContext, mPackageManager, mUserId,
     71                     onlyAppInfo.getComponentName());
     72         }
     73         return null;
     74     }
     75 
     76     private ActivityInfo getOnlyAppInfo(List<ResolveInfo> homeActivities) {
     77         final List<ActivityInfo> appLabels = new ArrayList<>();
     78 
     79         mPackageManager.getHomeActivities(homeActivities);
     80         for (ResolveInfo candidate : homeActivities) {
     81             final ActivityInfo info = candidate.activityInfo;
     82             if (info.packageName.equals(mPackageName)) {
     83                 continue;
     84             }
     85             appLabels.add(info);
     86         }
     87         return appLabels.size() == 1
     88                 ? appLabels.get(0)
     89                 : null;
     90     }
     91 
     92     @Override
     93     protected Intent getSettingIntent(DefaultAppInfo info) {
     94         if (info == null) {
     95             return null;
     96         }
     97         final String packageName;
     98         if (info.componentName != null) {
     99             packageName = info.componentName.getPackageName();
    100         } else if (info.packageItemInfo != null) {
    101             packageName = info.packageItemInfo.packageName;
    102         } else {
    103             return null;
    104         }
    105 
    106         Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
    107                 .setPackage(packageName)
    108                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    109         return mPackageManager.queryIntentActivities(intent, 0).size() == 1 ? intent : null;
    110     }
    111 
    112     public static boolean hasHomePreference(String pkg, Context context) {
    113         ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
    114         PackageManager pm = context.getPackageManager();
    115         pm.getHomeActivities(homeActivities);
    116         for (int i = 0; i < homeActivities.size(); i++) {
    117             if (homeActivities.get(i).activityInfo.packageName.equals(pkg)) {
    118                 return true;
    119             }
    120         }
    121         return false;
    122     }
    123 
    124     public static boolean isHomeDefault(String pkg, PackageManagerWrapper pm) {
    125         final ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
    126         ComponentName def = pm.getHomeActivities(homeActivities);
    127 
    128         return def == null || def.getPackageName().equals(pkg);
    129     }
    130 }
    131