1 /*
2  * Copyright (C) 2017 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 package com.android.settings.deviceinfo;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.text.TextUtils;
21 
22 import androidx.fragment.app.Fragment;
23 import androidx.preference.Preference;
24 
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settingslib.DeviceInfoUtils;
27 import com.android.settingslib.core.AbstractPreferenceController;
28 
29 public class FeedbackPreferenceController extends AbstractPreferenceController implements
30         PreferenceControllerMixin {
31     private static final String KEY_DEVICE_FEEDBACK = "device_feedback";
32 
33     private final Fragment mHost;
34     private final Intent intent;
35 
FeedbackPreferenceController(Fragment host, Context context)36     public FeedbackPreferenceController(Fragment host, Context context) {
37         super(context);
38         this.mHost = host;
39         intent = new Intent("android.intent.action.BUG_REPORT");
40     }
41 
42     @Override
isAvailable()43     public boolean isAvailable() {
44         return !TextUtils.isEmpty(DeviceInfoUtils.getFeedbackReporterPackage(mContext));
45     }
46 
47     @Override
updateState(Preference preference)48     public void updateState(Preference preference) {
49         super.updateState(preference);
50         intent.setPackage(DeviceInfoUtils.getFeedbackReporterPackage(mContext));
51         preference.setIntent(intent);
52 
53         // In some cases, cannot retrieve the report package from package manager,
54         // For example, launched from lock screen.
55         // Update this preference visibility after updateState.
56         if (isAvailable() && !preference.isVisible()) {
57             preference.setVisible(true);
58         } else if (!isAvailable() && preference.isVisible()){
59             preference.setVisible(false);
60         }
61     }
62 
getPreferenceKey()63     public String getPreferenceKey() {
64         return KEY_DEVICE_FEEDBACK;
65     }
66 
handlePreferenceTreeClick(Preference preference)67     public boolean handlePreferenceTreeClick(Preference preference) {
68         if (!TextUtils.equals(preference.getKey(), KEY_DEVICE_FEEDBACK)) {
69             return false;
70         }
71         if (!this.isAvailable()) {
72             return false;
73         }
74 
75         this.mHost.startActivityForResult(intent, 0);
76         return true;
77     }
78 }
79 
80