24
24
import android .content .Intent ;
25
25
import android .os .Handler ;
26
26
import android .os .Looper ;
27
+ import android .text .format .Formatter ;
27
28
import android .util .AttributeSet ;
28
29
import android .util .FloatProperty ;
29
30
import android .view .Gravity ;
@@ -74,12 +75,18 @@ public void setValue(MemInfoView view, float v) {
74
75
75
76
private String mMemInfoText ;
76
77
78
+ private ActivityManager .MemoryInfo memInfo ;
79
+
80
+ private Context mContext ;
81
+
77
82
public MemInfoView (Context context , AttributeSet attrs ) {
78
83
super (context , attrs );
79
84
85
+ mContext = context ;
80
86
mAlpha = new MultiValueAlpha (this , 2 );
81
87
mAlpha .setUpdateVisibility (true );
82
88
mActivityManager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
89
+ memInfo = new ActivityManager .MemoryInfo ();
83
90
mHandler = new Handler (Looper .getMainLooper ());
84
91
mWorker = new MemInfoWorker ();
85
92
@@ -132,24 +139,6 @@ else if (mDp.isTaskbarPresent && !((mode == THREE_BUTTONS) || (mode == TWO_BUTTO
132
139
lp .gravity = Gravity .CENTER_HORIZONTAL | Gravity .BOTTOM ;
133
140
}
134
141
135
- private String unitConvert (long valueMiB , boolean alignToGB ) {
136
- BigDecimal rawVal = new BigDecimal (valueMiB );
137
-
138
- if (alignToGB )
139
- return rawVal .divide (GB2MB , 0 , RoundingMode .UP ) + " GB" ;
140
-
141
- if (valueMiB > UNIT_CONVERT_THRESHOLD )
142
- return rawVal .divide (GB2MB , 1 , RoundingMode .HALF_UP ) + " GB" ;
143
- else
144
- return rawVal + " MB" ;
145
- }
146
-
147
- private void updateMemInfoText (long availMemMiB , long totalMemMiB ) {
148
- String text = String .format (mMemInfoText ,
149
- unitConvert (availMemMiB , false ), unitConvert (totalMemMiB , true ));
150
- setText (text );
151
- }
152
-
153
142
public void setListener (Context context ) {
154
143
setOnClickListener (view -> {
155
144
Intent intent = new Intent (Intent .ACTION_MAIN );
@@ -162,12 +151,13 @@ public void setListener(Context context) {
162
151
private class MemInfoWorker implements Runnable {
163
152
@ Override
164
153
public void run () {
165
- ActivityManager .MemoryInfo memInfo = new ActivityManager .MemoryInfo ();
166
154
mActivityManager .getMemoryInfo (memInfo );
167
- long availMemMiB = memInfo .availMem / (1024 * 1024 );
168
- long totalMemMiB = memInfo .totalMem / (1024 * 1024 );
169
- updateMemInfoText (availMemMiB , totalMemMiB );
170
-
155
+ String availResult = Formatter .formatShortFileSize (mContext ,
156
+ (long ) memInfo .availMem );
157
+ String totalResult = Formatter .formatShortFileSize (mContext ,
158
+ (long ) memInfo .totalMem );
159
+ String text = String .format (mMemInfoText , availResult , totalResult );
160
+ setText (text );
171
161
mHandler .postDelayed (this , 1000 );
172
162
}
173
163
}
0 commit comments