Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2014 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.tv.settings;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.pm.ApplicationInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.ResolveInfo;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v7.preference.PreferenceGroup;
     26 
     27 import java.util.List;
     28 
     29 /**
     30  * Utilities for saving and loading shared prefs.
     31  */
     32 public final class PreferenceUtils {
     33 
     34     public static final int FLAG_SET_TITLE = 1;
     35 
     36     public static void resolveSystemActivityOrRemove(Context context, PreferenceGroup parent,
     37             Preference preference, int flags) {
     38         if (preference == null) {
     39             return;
     40         }
     41 
     42         Intent intent = preference.getIntent();
     43         if (intent != null) {
     44             // Find the activity that is in the system image
     45             PackageManager pm = context.getPackageManager();
     46             List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
     47             for (final ResolveInfo resolveInfo : list) {
     48                 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
     49                         != 0) {
     50 
     51                     // Replace the intent with this specific activity
     52                     preference.setIntent(new Intent().setClassName(
     53                             resolveInfo.activityInfo.packageName,
     54                             resolveInfo.activityInfo.name));
     55 
     56                     if ((flags & FLAG_SET_TITLE) != 0) {
     57                         // Set the preference title to the activity's label
     58                         preference.setTitle(resolveInfo.loadLabel(pm));
     59                     }
     60 
     61                     return;
     62                 }
     63             }
     64         }
     65 
     66         // Did not find a matching activity, so remove the preference
     67         parent.removePreference(preference);
     68     }
     69 }
     70