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 
17 package com.android.settingslib.deviceinfo;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.os.SystemClock;
23 import android.text.format.DateUtils;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settingslib.core.AbstractPreferenceController;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnStart;
33 import com.android.settingslib.core.lifecycle.events.OnStop;
34 
35 import java.lang.ref.WeakReference;
36 
37 /**
38  * Preference controller for uptime
39  */
40 public abstract class AbstractUptimePreferenceController extends AbstractPreferenceController
41         implements LifecycleObserver, OnStart, OnStop {
42 
43     @VisibleForTesting
44     static final String KEY_UPTIME = "up_time";
45     private static final int EVENT_UPDATE_STATS = 500;
46 
47     private Preference mUptime;
48     private Handler mHandler;
49 
AbstractUptimePreferenceController(Context context, Lifecycle lifecycle)50     public AbstractUptimePreferenceController(Context context, Lifecycle lifecycle) {
51         super(context);
52         if (lifecycle != null) {
53             lifecycle.addObserver(this);
54         }
55     }
56 
57     @Override
onStart()58     public void onStart() {
59         getHandler().sendEmptyMessage(EVENT_UPDATE_STATS);
60     }
61 
62     @Override
onStop()63     public void onStop() {
64         getHandler().removeMessages(EVENT_UPDATE_STATS);
65     }
66 
67     @Override
isAvailable()68     public boolean isAvailable() {
69         return true;
70     }
71 
72     @Override
getPreferenceKey()73     public String getPreferenceKey() {
74         return KEY_UPTIME;
75     }
76 
77     @Override
displayPreference(PreferenceScreen screen)78     public void displayPreference(PreferenceScreen screen) {
79         super.displayPreference(screen);
80         mUptime = screen.findPreference(KEY_UPTIME);
81         updateTimes();
82     }
83 
getHandler()84     private Handler getHandler() {
85         if (mHandler == null) {
86             mHandler = new MyHandler(this);
87         }
88         return mHandler;
89     }
90 
updateTimes()91     private void updateTimes() {
92         mUptime.setSummary(DateUtils.formatElapsedTime(SystemClock.elapsedRealtime() / 1000));
93     }
94 
95     private static class MyHandler extends Handler {
96         private WeakReference<AbstractUptimePreferenceController> mStatus;
97 
MyHandler(AbstractUptimePreferenceController activity)98         public MyHandler(AbstractUptimePreferenceController activity) {
99             mStatus = new WeakReference<>(activity);
100         }
101 
102         @Override
handleMessage(Message msg)103         public void handleMessage(Message msg) {
104             AbstractUptimePreferenceController status = mStatus.get();
105             if (status == null) {
106                 return;
107             }
108 
109             switch (msg.what) {
110                 case EVENT_UPDATE_STATS:
111                     status.updateTimes();
112                     sendEmptyMessageDelayed(EVENT_UPDATE_STATS, 1000);
113                     break;
114 
115                 default:
116                     throw new IllegalStateException("Unknown message " + msg.what);
117             }
118         }
119     }
120 }
121