1 /*
2  * Copyright (C) 2023 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.privacy;
18 
19 import static android.safetylabel.SafetyLabelConstants.SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.provider.DeviceConfig;
25 
26 import androidx.annotation.NonNull;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.core.BasePreferenceController;
31 
32 /**
33  * PreferenceController which hides the Data Sharing update if safety labels aren't enabled
34  * TODO b/264939792: Add tests
35  */
36 public class AppDataSharingUpdatesPreferenceController extends BasePreferenceController {
AppDataSharingUpdatesPreferenceController(Context context, String preferenceKey)37     public AppDataSharingUpdatesPreferenceController(Context context,
38             String preferenceKey) {
39         super(context, preferenceKey);
40     }
41 
42     @Override
displayPreference(@onNull PreferenceScreen screen)43     public void displayPreference(@NonNull PreferenceScreen screen) {
44         super.displayPreference(screen);
45 
46         Preference pref = screen.findPreference(getPreferenceKey());
47         if (pref != null) {
48             pref.setIntent(new Intent(Intent.ACTION_REVIEW_APP_DATA_SHARING_UPDATES)
49                     .setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
50         }
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         return isPrivacySafetyLabelChangeNotificationsEnabled(mContext)
56                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
57     }
58 
isPrivacySafetyLabelChangeNotificationsEnabled(Context context)59     private boolean isPrivacySafetyLabelChangeNotificationsEnabled(Context context) {
60         PackageManager packageManager = context.getPackageManager();
61         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
62                     SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED, true)
63                 && !packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)
64                 && !packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
65                 && !packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH);
66     }
67 }
68