Android P 添加存储容量显示和已开机时间选项

在这里插入图片描述
vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values-zh-rCN/strings.xml

 <string name="storage_info">存储容量</string>   
    <string name="storage_total">总共容量: </string>
    <string name="storage_free">剩余空间: </string>

vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/my_device_info.xml

 <!-- add storage -->
    <Preference
        android:key="storage_info"
        android:order="53"
        android:title="@string/storage_info"
        android:summary="@string/summary_placeholder"/>

    <!--  add uptime -->    
    <Preference
        android:enabled="false"
        android:key="up_time"
        android:order="54"
        android:persistent="false"
        android:shouldDisableView="false"
        android:summary="@string/summary_placeholder"
        android:title="@string/status_up_time" /> 

vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/deviceinfo/aboutphone/MyDeviceInfoFragment.java

 import com.android.settings.deviceinfo.imei.ImeiInfoPreferenceController;
 import com.android.settings.deviceinfo.simstatus.SimStatusPreferenceController;
+import com.android.settings.deviceinfo.StorageInfoPreferenceController;// add storage
+import com.android.settings.deviceinfo.UpdateTimePreferenceController;// add uptime
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.widget.EntityHeaderController;
 import com.android.settingslib.core.AbstractPreferenceController;
@@ -68,6 +70,7 @@ public class MyDeviceInfoFragment extends DashboardFragment
 
     private static final String KEY_MY_DEVICE_INFO_HEADER = "my_device_info_header";
     private static final String KEY_LEGAL_CONTAINER = "legal_container";
+    private static UpdateTimePreferenceController mUpdateTimePreferenceController;//add uptime
 
     @Override
     public int getMetricsCategory() {
@@ -82,9 +85,18 @@ public class MyDeviceInfoFragment extends DashboardFragment
     @Override
     public void onResume() {
         super.onResume();
+        mUpdateTimePreferenceController.onResume();/ add uptime
         initHeader();
     }
 
+    // add uptime
+    @Override
+    public void onPause() {
+        super.onPause();
+        mUpdateTimePreferenceController.onPause();
+    }
+    // end uptime
+
     @Override
     protected String getLogTag() {
         return LOG_TAG;
@@ -109,6 +121,9 @@ public class MyDeviceInfoFragment extends DashboardFragment
         final List<AbstractPreferenceController> controllers = new ArrayList<>();
         controllers.add(new EmergencyInfoPreferenceController(context));
         /// M: Revise for updating phone number.
+        mUpdateTimePreferenceController = new UpdateTimePreferenceController(context);// add uptime
+        controllers.add(mUpdateTimePreferenceController);// add uptime
+        controllers.add(new StorageInfoPreferenceController(context));//add storage
         controllers.add(new PhoneNumberPreferenceController(context, lifecycle));
         controllers.add(new BrandedAccountPreferenceController(context));

添加vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/deviceinfo/StorageInfoPreferenceController.java

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.deviceinfo;

import java.text.NumberFormat;

import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.R;
import android.content.Context;
import android.content.res.Resources;
import android.os.storage.StorageManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.format.Formatter;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
import com.android.settingslib.deviceinfo.PrivateStorageInfo;

public class StorageInfoPreferenceController extends AbstractPreferenceController implements

		PreferenceControllerMixin {
	private static final String KEY_STORAGE_INFO = "storage_info";
	private StorageManagerVolumeProvider mStorageManagerVolumeProvider;
	private Context mContext;

	public StorageInfoPreferenceController(Context context) {
		super(context);
		mContext = context;
	}

	@Override
	public boolean isAvailable() {
		return true;
	}

	@Override
	public void displayPreference(PreferenceScreen screen) {
		super.displayPreference(screen);
		final Preference pref = screen.findPreference(KEY_STORAGE_INFO);
		if (pref != null) {
			pref.setSummary(getStorageInfo());
		}
	}

	@Override
	public String getPreferenceKey() {
		return KEY_STORAGE_INFO;
	}

	@Override
	public boolean handlePreferenceTreeClick(Preference preference) {
		return false;
	}

	private String getStorageInfo() {
		StorageManager storageManager = mContext.getSystemService(StorageManager.class);
		mStorageManagerVolumeProvider = new StorageManagerVolumeProvider(storageManager);

		NumberFormat percentageFormat = NumberFormat.getPercentInstance();
		PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(mStorageManagerVolumeProvider);
		double privateUsedBytes = info.totalBytes - info.freeBytes;
		StringBuilder builder = new StringBuilder();
		Resources res = mContext.getResources();
		builder.append(res.getString(R.string.storage_total))
				.append(Formatter.formatFileSize(mContext, info.totalBytes)).append("  ")
				.append(res.getString(R.string.storage_free))
				.append(Formatter.formatFileSize(mContext, info.freeBytes));
		return builder.toString();
	}

}

添加vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/deviceinfo/UpdateTimePreferenceController.java

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.deviceinfo;

import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.android.internal.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;

import android.app.ActivityManager;
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.os.storage.StorageManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.format.DateFormat;

public class UpdateTimePreferenceController extends AbstractPreferenceController implements

		PreferenceControllerMixin {

	private static final String KEY_UPDATE_TIME = "up_time";
	private static final int EVENT_UPDATE_STATS = 500;

	private Context mContext;
	private Preference mPref;
	private MyHandler mMyHandler;

	public UpdateTimePreferenceController(Context context) {
		super(context);
		mContext = context;
		mMyHandler = new MyHandler(this);
	}

	@Override
	public boolean isAvailable() {
		return true;
	}

	@Override
	public void displayPreference(PreferenceScreen screen) {
		super.displayPreference(screen);
		mPref = screen.findPreference(KEY_UPDATE_TIME);

	}

	@Override
	public String getPreferenceKey() {
		return KEY_UPDATE_TIME;
	}

	@Override
	public boolean handlePreferenceTreeClick(Preference preference) {
		return false;
	}

	public void onResume() {
		mMyHandler.sendEmptyMessage(EVENT_UPDATE_STATS);
	}

	public void onPause() {
		mMyHandler.removeMessages(EVENT_UPDATE_STATS);
	}

	private void updateTime() {
		long at = SystemClock.uptimeMillis() / 1000;
		long ut = SystemClock.elapsedRealtime() / 1000;
		if (ut == 0) {
			ut = 1;
		}
		if (mPref != null) {
			mPref.setSummary(convert(ut));
		}
	}

	private String pad(int n) {
		if (n >= 10) {
			return String.valueOf(n);
		} else {
			return "0" + String.valueOf(n);
		}
	}

	private String convert(long t) {
		int s = (int) (t % 60);
		int m = (int) ((t / 60) % 60);
		int h = (int) ((t / 3600));

		return h + ":" + pad(m) + ":" + pad(s);
	}

	private static class MyHandler extends Handler {

		UpdateTimePreferenceController controller;

		public MyHandler(UpdateTimePreferenceController controller) {
			super(Looper.getMainLooper());
			this.controller = controller;
		}

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case EVENT_UPDATE_STATS:
				controller.updateTime();
				sendEmptyMessageDelayed(EVENT_UPDATE_STATS, 1000);
				break;

			}
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最乱纷飞的code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值