1 /*
2  * Copyright (C) 2019 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.car.developeroptions.applications.appinfo;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.UserManager;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.car.developeroptions.R;
26 import com.android.car.developeroptions.Utils;
27 import com.android.car.developeroptions.applications.AppStoreUtil;
28 import com.android.settingslib.applications.AppUtils;
29 
30 public class AppInstallerInfoPreferenceController extends AppInfoPreferenceControllerBase {
31 
32     private String mPackageName;
33     private String mInstallerPackage;
34     private CharSequence mInstallerLabel;
35 
AppInstallerInfoPreferenceController(Context context, String key)36     public AppInstallerInfoPreferenceController(Context context, String key) {
37         super(context, key);
38     }
39 
40     @Override
getAvailabilityStatus()41     public int getAvailabilityStatus() {
42         if (UserManager.get(mContext).isManagedProfile()) {
43             return DISABLED_FOR_USER;
44         }
45         return mInstallerLabel != null ? AVAILABLE : DISABLED_FOR_USER;
46     }
47 
48     @Override
updateState(Preference preference)49     public void updateState(Preference preference) {
50         final int detailsStringId = AppUtils.isInstant(mParent.getPackageInfo().applicationInfo)
51                 ? R.string.instant_app_details_summary
52                 : R.string.app_install_details_summary;
53         preference.setSummary(mContext.getString(detailsStringId, mInstallerLabel));
54 
55         Intent intent = AppStoreUtil.getAppStoreLink(mContext, mInstallerPackage, mPackageName);
56         if (intent != null) {
57             preference.setIntent(intent);
58         } else {
59             preference.setEnabled(false);
60         }
61     }
62 
setPackageName(String packageName)63     public void setPackageName(String packageName) {
64         mPackageName = packageName;
65         mInstallerPackage = AppStoreUtil.getInstallerPackageName(mContext, mPackageName);
66         mInstallerLabel = Utils.getApplicationLabel(mContext, mInstallerPackage);
67     }
68 }
69