1 /*
2  * Copyright (C) 2021 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 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.text.TextUtils;
25 import android.util.Log;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 
30 import com.android.settings.SettingsPreferenceFragment;
31 import com.android.settings.applications.AppLocaleUtil;
32 import com.android.settings.localepicker.AppLocalePickerActivity;
33 
34 import java.util.List;
35 
36 /**
37  * A controller to update current locale information of application.
38  */
39 public class AppLocalePreferenceController extends AppInfoPreferenceControllerBase {
40     private static final String TAG = AppLocalePreferenceController.class.getSimpleName();
41 
42     private final List<ResolveInfo> mListInfos;
43 
AppLocalePreferenceController(Context context, String key)44     public AppLocalePreferenceController(Context context, String key) {
45         super(context, key);
46         mListInfos = context.getPackageManager().queryIntentActivities(
47                 AppLocaleUtil.LAUNCHER_ENTRY_INTENT, PackageManager.GET_META_DATA);
48     }
49 
50     @Override
getAvailabilityStatus()51     public int getAvailabilityStatus() {
52         return canDisplayLocaleUi() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
53     }
54 
55     @Override
getDetailFragmentClass()56     protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
57         return AppLocaleDetails.class;
58     }
59 
60     @Override
getSummary()61     public CharSequence getSummary() {
62         return AppLocaleDetails.getSummary(mContext, mParent.getAppEntry().info);
63     }
64 
65     @Override
handlePreferenceTreeClick(Preference preference)66     public boolean handlePreferenceTreeClick(Preference preference) {
67         if (!TextUtils.equals(preference.getKey(), mPreferenceKey)) {
68             return false;
69         }
70 
71         if (mParent != null) {
72             Intent intent = new Intent(mContext, AppLocalePickerActivity.class);
73             intent.setData(Uri.parse("package:" + mParent.getAppEntry().info.packageName));
74             mContext.startActivity(intent);
75             return true;
76         } else {
77             Log.d(TAG, "mParent is null");
78             return false;
79         }
80     }
81 
82     @VisibleForTesting
canDisplayLocaleUi()83     boolean canDisplayLocaleUi() {
84         return AppLocaleUtil
85                 .canDisplayLocaleUi(mContext, mParent.getAppEntry().info, mListInfos);
86     }
87 }
88