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