Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d61d1a49 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactor status page to DashboardFragment"

parents 921db1f9 f63ad8f5
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
 */
package com.android.settings.core;

import android.text.TextUtils;
import android.util.Log;

import com.android.settings.search.ResultPayload;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -26,6 +29,8 @@ import java.util.List;
 */
public interface PreferenceControllerMixin {

    String TAG = "PrefControllerMixin";

    /**
     * Updates non-indexable keys for search provider.
     *
@@ -34,7 +39,13 @@ public interface PreferenceControllerMixin {
    default void updateNonIndexableKeys(List<String> keys) {
        if (this instanceof AbstractPreferenceController) {
            if (!((AbstractPreferenceController) this).isAvailable()) {
                keys.add(((AbstractPreferenceController) this).getPreferenceKey());
                final String key = ((AbstractPreferenceController) this).getPreferenceKey();
                if (TextUtils.isEmpty(key)) {
                    Log.w(TAG,
                            "Skipping updateNonIndexableKeys due to empty key " + this.toString());
                    return;
                }
                keys.add(key);
            }
        }
    }
+104 −0
Original line number Diff line number Diff line
/*
 * 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 android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;

public class BatteryInfoPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {

    @VisibleForTesting
    static final IntentFilter BATTERY_INFO_RECEIVER_INTENT_FILTER =
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    @VisibleForTesting
    static final String KEY_BATTERY_STATUS = "battery_status";
    @VisibleForTesting
    static final String KEY_BATTERY_LEVEL = "battery_level";

    @VisibleForTesting
    BroadcastReceiver mBatteryInfoReceiver;
    private Preference mBatteryStatus;
    private Preference mBatteryLevel;


    public BatteryInfoPreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        mBatteryInfoReceiver = new BatteryInfoReceiver(context);
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }

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

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

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mBatteryLevel = screen.findPreference(KEY_BATTERY_LEVEL);
        mBatteryStatus = screen.findPreference(KEY_BATTERY_STATUS);
    }

    @Override
    public void onStart() {
        mContext.registerReceiver(mBatteryInfoReceiver, BATTERY_INFO_RECEIVER_INTENT_FILTER);
    }

    @Override
    public void onStop() {
        mContext.unregisterReceiver(mBatteryInfoReceiver);
    }

    private class BatteryInfoReceiver extends BroadcastReceiver {

        private final Context mContext;

        public BatteryInfoReceiver(Context context) {
            mContext = context;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent));
                mBatteryStatus.setSummary(Utils.getBatteryStatus(mContext.getResources(), intent));
            }
        }
    }
}
+45 −0
Original line number Diff line number Diff line
/*
 * 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 android.content.Context;
import android.os.UserManager;

import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;

public class ImeiInfoPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin {

    private static final String KEY_IMEI_INFO = "imei_info";

    public ImeiInfoPreferenceController(Context context) {
        super(context);
    }

    @Override
    public boolean isAvailable() {
        return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser()
                && !Utils.isWifiOnly(mContext);
    }

    @Override
    public String getPreferenceKey() {
        return KEY_IMEI_INFO;
    }
}
+45 −0
Original line number Diff line number Diff line
/*
 * 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 android.content.Context;
import android.os.UserManager;

import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;

public class SimStatusPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin {

    private static final String KEY_SIM_STATUS = "sim_status";

    public SimStatusPreferenceController(Context context) {
        super(context);
    }

    @Override
    public boolean isAvailable() {
        return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser()
                && !Utils.isWifiOnly(mContext);
    }

    @Override
    public String getPreferenceKey() {
        return KEY_SIM_STATUS;
    }
}
+32 −90
Original line number Diff line number Diff line
@@ -16,95 +16,26 @@

package com.android.settings.deviceinfo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Fragment for showing device hardware info, such as MAC addresses and serial numbers
 */
public class Status extends SettingsPreferenceFragment implements Indexable {
public class Status extends DashboardFragment {

    private static final String KEY_BATTERY_STATUS = "battery_status";
    private static final String KEY_BATTERY_LEVEL = "battery_level";
    private static final String KEY_SIM_STATUS = "sim_status";
    private static final String KEY_IMEI_INFO = "imei_info";

    private SerialNumberPreferenceController mSerialNumberPreferenceController;
    private UptimePreferenceController mUptimePreferenceController;
    private Preference mBatteryStatus;
    private Preference mBatteryLevel;
    private BluetoothAddressPreferenceController mBluetoothAddressPreferenceController;
    private IpAddressPreferenceController mIpAddressPreferenceController;
    private WifiMacAddressPreferenceController mWifiMacAddressPreferenceController;
    private ImsStatusPreferenceController mImsStatusPreferenceController;

    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent));
                mBatteryStatus.setSummary(Utils.getBatteryStatus(getResources(), intent));
            }
        }
    };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        final Context context = getContext();
        final Lifecycle lifecycle = getLifecycle();
        mSerialNumberPreferenceController = new SerialNumberPreferenceController(context);
        mUptimePreferenceController = new UptimePreferenceController(context, lifecycle);
        mBluetoothAddressPreferenceController =
                new BluetoothAddressPreferenceController(context, lifecycle);
        mIpAddressPreferenceController = new IpAddressPreferenceController(context, lifecycle);
        mWifiMacAddressPreferenceController =
                new WifiMacAddressPreferenceController(context, lifecycle);
        mImsStatusPreferenceController = new ImsStatusPreferenceController(context, lifecycle);

        addPreferencesFromResource(R.xml.device_info_status);
        mBatteryLevel = findPreference(KEY_BATTERY_LEVEL);
        mBatteryStatus = findPreference(KEY_BATTERY_STATUS);

        final PreferenceScreen screen = getPreferenceScreen();

        mSerialNumberPreferenceController.displayPreference(screen);
        mUptimePreferenceController.displayPreference(screen);
        mBluetoothAddressPreferenceController.displayPreference(screen);
        mIpAddressPreferenceController.displayPreference(screen);
        mWifiMacAddressPreferenceController.displayPreference(screen);
        mImsStatusPreferenceController.displayPreference(screen);

        // Remove SimStatus and Imei for Secondary user as it access Phone b/19165700
        // Also remove on Wi-Fi only devices.
        //TODO: the bug above will surface in split system user mode.
        if (!UserManager.get(getContext()).isAdminUser()
                || Utils.isWifiOnly(getContext())) {
            removePreferenceFromScreen(KEY_SIM_STATUS);
            removePreferenceFromScreen(KEY_IMEI_INFO);
        }
    }
    private static final String TAG = "DeviceStatus";

    @Override
    public int getMetricsCategory() {
@@ -112,28 +43,33 @@ public class Status extends SettingsPreferenceFragment implements Indexable {
    }

    @Override
    public void onResume() {
        super.onResume();
        getContext().registerReceiver(mBatteryInfoReceiver,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    protected String getLogTag() {
        return TAG;
    }

    @Override
    public void onPause() {
        super.onPause();

        getContext().unregisterReceiver(mBatteryInfoReceiver);
    protected int getPreferenceScreenResId() {
        return R.xml.device_info_status;
    }

    /**
     * Removes the specified preference, if it exists.
     * @param key the key for the Preference item
     */
    private void removePreferenceFromScreen(String key) {
        Preference pref = findPreference(key);
        if (pref != null) {
            getPreferenceScreen().removePreference(pref);
    @Override
    protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
        return buildPreferenceControllers(context, getLifecycle());
    }

    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
            Lifecycle lifecycle) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(new SerialNumberPreferenceController(context));
        controllers.add(new UptimePreferenceController(context, lifecycle));
        controllers.add(new BluetoothAddressPreferenceController(context, lifecycle));
        controllers.add(new IpAddressPreferenceController(context, lifecycle));
        controllers.add(new WifiMacAddressPreferenceController(context, lifecycle));
        controllers.add(new ImsStatusPreferenceController(context, lifecycle));
        controllers.add(new SimStatusPreferenceController(context));
        controllers.add(new ImeiInfoPreferenceController(context));
        controllers.add(new BatteryInfoPreferenceController(context, lifecycle));
        return controllers;
    }

    /**
@@ -149,5 +85,11 @@ public class Status extends SettingsPreferenceFragment implements Indexable {
                    sir.xmlResId = R.xml.device_info_status;
                    return Arrays.asList(sir);
                }

                @Override
                public List<AbstractPreferenceController> getPreferenceControllers(Context
                        context) {
                    return buildPreferenceControllers(context, null /* lifecycle */);
                }
            };
}
Loading