Home | History | Annotate | Download | only in applications
      1 /*
      2  * Copyright (C) 2015 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;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.content.pm.ResolveInfo;
     23 import android.net.Uri;
     24 import android.os.UserHandle;
     25 import android.util.AttributeSet;
     26 
     27 import com.android.settings.AppListPreference;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 public class DefaultBrowserPreference extends AppListPreference {
     33 
     34     final private PackageManager mPm;
     35 
     36     public DefaultBrowserPreference(Context context, AttributeSet attrs) {
     37         super(context, attrs);
     38 
     39         mPm = context.getPackageManager();
     40         refreshBrowserApps();
     41     }
     42 
     43     public void refreshBrowserApps() {
     44         List<String> browsers = resolveBrowserApps();
     45 
     46         setPackageNames(browsers.toArray(new String[browsers.size()]), null);
     47     }
     48 
     49     private List<String> resolveBrowserApps() {
     50         List<String> result = new ArrayList<>();
     51 
     52         // Create an Intent that will match ALL Browser Apps
     53         Intent intent = new Intent();
     54         intent.setAction(Intent.ACTION_VIEW);
     55         intent.addCategory(Intent.CATEGORY_BROWSABLE);
     56         intent.setData(Uri.parse("http:"));
     57 
     58         // Resolve that intent and check that the handleAllWebDataURI boolean is set
     59         List<ResolveInfo> list = mPm.queryIntentActivitiesAsUser(intent, PackageManager.MATCH_ALL,
     60                 UserHandle.myUserId());
     61 
     62         final int count = list.size();
     63         for (int i=0; i<count; i++) {
     64             ResolveInfo info = list.get(i);
     65             if (info.activityInfo == null || result.contains(info.activityInfo.packageName)
     66                     || !info.handleAllWebDataURI) {
     67                 continue;
     68             }
     69 
     70             result.add(info.activityInfo.packageName);
     71         }
     72 
     73         return result;
     74     }
     75 }
     76