1 /* 2 * Copyright (C) 2018 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.appinfo; 18 19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_OPEN_APP_SETTING; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ResolveInfo; 24 import android.support.v7.preference.Preference; 25 import android.text.TextUtils; 26 27 import com.android.settings.overlay.FeatureFactory; 28 29 public class AppSettingPreferenceController extends AppInfoPreferenceControllerBase { 30 31 private String mPackageName; 32 33 public AppSettingPreferenceController(Context context, String preferenceKey) { 34 super(context, preferenceKey); 35 } 36 37 public AppSettingPreferenceController setPackageName(String packageName) { 38 mPackageName = packageName; 39 return this; 40 } 41 42 @Override 43 public int getAvailabilityStatus() { 44 if (TextUtils.isEmpty(mPackageName) || mParent == null) { 45 return CONDITIONALLY_UNAVAILABLE; 46 } 47 final Intent intent = resolveIntent( 48 new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(mPackageName)); 49 return intent != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 50 } 51 52 @Override 53 public boolean handlePreferenceTreeClick(Preference preference) { 54 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { 55 return false; 56 } 57 final Intent intent = resolveIntent( 58 new Intent(Intent.ACTION_APPLICATION_PREFERENCES).setPackage(mPackageName)); 59 if (intent == null) { 60 return false; 61 } 62 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider() 63 .actionWithSource(mContext, mParent.getMetricsCategory(), 64 ACTION_OPEN_APP_SETTING); 65 mContext.startActivity(intent); 66 return true; 67 } 68 69 private Intent resolveIntent(Intent i) { 70 ResolveInfo result = mContext.getPackageManager().resolveActivity(i, 0); 71 if (result != null) { 72 return new Intent(i.getAction()) 73 .setClassName(result.activityInfo.packageName, result.activityInfo.name); 74 } 75 return null; 76 } 77 } 78