blob: d94586be094af135afef260ce9207e26916eb849 [file] [log] [blame]
Fan Zhangefb8d622017-02-23 17:50:27 -08001/*
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 */
Fan Zhang69e95c62017-02-23 16:05:30 -080016package com.android.settings.deviceinfo;
17
Fan Zhang69e95c62017-02-23 16:05:30 -080018import android.content.Context;
19import android.content.Intent;
Fan Zhang69e95c62017-02-23 16:05:30 -080020import android.text.TextUtils;
21
Fan Zhang23f8d592018-08-28 15:11:40 -070022import androidx.fragment.app.Fragment;
23import androidx.preference.Preference;
24
Tony Mantler1d583e12017-06-13 13:09:25 -070025import com.android.settings.core.PreferenceControllerMixin;
Fan Zhang69e95c62017-02-23 16:05:30 -080026import com.android.settingslib.DeviceInfoUtils;
Tony Mantler1d583e12017-06-13 13:09:25 -070027import com.android.settingslib.core.AbstractPreferenceController;
Fan Zhang69e95c62017-02-23 16:05:30 -080028
Tony Mantler1d583e12017-06-13 13:09:25 -070029public class FeedbackPreferenceController extends AbstractPreferenceController implements
30 PreferenceControllerMixin {
Fan Zhang69e95c62017-02-23 16:05:30 -080031 private static final String KEY_DEVICE_FEEDBACK = "device_feedback";
Fan Zhangefb8d622017-02-23 17:50:27 -080032
Fan Zhang69e95c62017-02-23 16:05:30 -080033 private final Fragment mHost;
Fan Zhangefb8d622017-02-23 17:50:27 -080034 private final Intent intent;
Fan Zhang69e95c62017-02-23 16:05:30 -080035
36 public FeedbackPreferenceController(Fragment host, Context context) {
37 super(context);
38 this.mHost = host;
Fan Zhangefb8d622017-02-23 17:50:27 -080039 intent = new Intent("android.intent.action.BUG_REPORT");
Fan Zhang69e95c62017-02-23 16:05:30 -080040 }
41
wayneyang77f5ad52018-03-21 20:05:09 +080042 @Override
Fan Zhang69e95c62017-02-23 16:05:30 -080043 public boolean isAvailable() {
Fan Zhangefb8d622017-02-23 17:50:27 -080044 return !TextUtils.isEmpty(DeviceInfoUtils.getFeedbackReporterPackage(mContext));
45 }
46
47 @Override
48 public void updateState(Preference preference) {
49 super.updateState(preference);
50 intent.setPackage(DeviceInfoUtils.getFeedbackReporterPackage(mContext));
51 preference.setIntent(intent);
wayneyang77f5ad52018-03-21 20:05:09 +080052
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 }
Fan Zhang69e95c62017-02-23 16:05:30 -080061 }
62
63 public String getPreferenceKey() {
64 return KEY_DEVICE_FEEDBACK;
65 }
66
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 }
Fan Zhangefb8d622017-02-23 17:50:27 -080074
Fan Zhang69e95c62017-02-23 16:05:30 -080075 this.mHost.startActivityForResult(intent, 0);
76 return true;
77 }
78}
79