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.fuelgauge.batterytip.tips;
18 
19 import android.app.Activity;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.os.Parcel;
23 import android.util.Log;
24 
25 import androidx.core.app.ActivityCompat;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settingslib.HelpUtils;
30 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
31 
32 import kotlin.Unit;
33 
34 /** Tip to show incompatible charger state */
35 public final class IncompatibleChargerTip extends BatteryTip {
36     private static final String TAG = "IncompatibleChargerTip";
37 
IncompatibleChargerTip(@tateType int state)38     public IncompatibleChargerTip(@StateType int state) {
39         super(TipType.INCOMPATIBLE_CHARGER, state, /* showDialog */ false);
40     }
41 
IncompatibleChargerTip(Parcel in)42     private IncompatibleChargerTip(Parcel in) {
43         super(in);
44     }
45 
46     @Override
getTitle(Context context)47     public CharSequence getTitle(Context context) {
48         return context.getString(R.string.battery_tip_incompatible_charging_title);
49     }
50 
51     @Override
getSummary(Context context)52     public CharSequence getSummary(Context context) {
53         return context.getString(R.string.battery_tip_incompatible_charging_message);
54     }
55 
56     @Override
getIconId()57     public int getIconId() {
58         return R.drawable.ic_battery_incompatible_charger;
59     }
60 
61     @Override
updateState(BatteryTip tip)62     public void updateState(BatteryTip tip) {
63         mState = tip.mState;
64     }
65 
66     @Override
log(Context context, MetricsFeatureProvider metricsFeatureProvider)67     public void log(Context context, MetricsFeatureProvider metricsFeatureProvider) {
68         metricsFeatureProvider.action(
69                 context, SettingsEnums.ACTION_INCOMPATIBLE_CHARGING_TIP, mState);
70     }
71 
72     @Override
updatePreference(Preference preference)73     public void updatePreference(Preference preference) {
74         super.updatePreference(preference);
75         final Context context = preference.getContext();
76         final var cardPreference = castToTipCardPreferenceSafely(preference);
77         if (cardPreference == null) {
78             Log.e(TAG, "cast Preference to CardPreference failed");
79             return;
80         }
81 
82         cardPreference.setSelectable(false);
83         cardPreference.enableDismiss(false);
84         cardPreference.setIconResId(getIconId());
85         cardPreference.setPrimaryButtonText(context.getString(R.string.learn_more));
86         cardPreference.setPrimaryButtonAction(
87                 () -> {
88                     var helpIntent =
89                             HelpUtils.getHelpIntent(
90                                     context,
91                                     context.getString(R.string.help_url_incompatible_charging),
92                                     /* backupContext */ "");
93                     ActivityCompat.startActivityForResult(
94                             (Activity) context,
95                             helpIntent,
96                             /* requestCode= */ 0,
97                             /* options= */ null);
98 
99                     return Unit.INSTANCE;
100                 });
101         cardPreference.setPrimaryButtonVisibility(true);
102         cardPreference.setPrimaryButtonContentDescription(
103                 context.getString(R.string.battery_tip_incompatible_charging_content_description));
104         cardPreference.buildContent();
105     }
106 
107     public static final Creator CREATOR =
108             new Creator() {
109                 public BatteryTip createFromParcel(Parcel in) {
110                     return new IncompatibleChargerTip(in);
111                 }
112 
113                 public BatteryTip[] newArray(int size) {
114                     return new IncompatibleChargerTip[size];
115                 }
116             };
117 }
118