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

Commit a12003a9 authored by SongFerng Wang's avatar SongFerng Wang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "Slice of Provider Model"

* changes:
  Slice of Provider Model: add slice structure
  The helper for slice of carrier and non-Carrier, used by ProviderModelSlice.
parents d41cb031 8cad0197
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright (C) 2020 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0"
    android:tint="?android:attr/colorControlNormal">
    <path
        android:fillColor="#FF000000"
        android:pathData="M2,22l16,0l0,-2l-11,0l13,-13l0,1l2,0l0,-6z"
        android:strokeAlpha="0.3"
        android:fillAlpha="0.3"/>
    <path
        android:fillColor="#FF000000"
        android:pathData="M20,10h2v8h-2z"/>
    <path
        android:fillColor="#FF000000"
        android:pathData="M20,20h2v2h-2z"/>
</vector>
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
@@ -12527,6 +12527,15 @@
    <!-- Label text to turn off airplane mode. [CHAR LIMIT=40] -->
    <string name="turn_off_airplane_mode">Turn off Airplane Mode</string>
    <!-- Provider Model: Summary indicating that a SIM has an active mobile data connection [CHAR LIMIT=50] -->
    <string name="mobile_data_connection_active">Active, <xliff:g id="network_data_rat" example="LTE">%1$s</xliff:g></string>
    <!-- Provider Model: Summary indicating that a SIM has an active mobile data connection [CHAR LIMIT=50] -->
    <string name="mobile_data_off_summary">Internet off</string>
    <!-- Provider Model: Summary indicating that non-carrier network unavailable [CHAR LIMIT=50] -->
    <string name="non_carrier_network_unavailable">Non\u2011carrier networks unavailable</string>
    <!-- Provider Model: Summary indicating that non-carrier and carrier network unavailable [CHAR LIMIT=50] -->
    <string name="all_network_unavailable">Networks unavailable</string>
    <!-- Summary for preference when Bedtime mode is on [CHAR LIMIT=NONE] -->
    <string name="aware_summary_when_bedtime_on">Unavailable because bedtime mode is on</string>
+0 −1
Original line number Diff line number Diff line
@@ -1156,5 +1156,4 @@ public final class Utils extends com.android.settingslib.Utils {
    public static boolean isProviderModelEnabled(Context context) {
        return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.SETTINGS_PROVIDER_MODEL);
    }

}
+3 −1
Original line number Diff line number Diff line
@@ -103,8 +103,10 @@ public class NetworkProviderSettings extends RestrictedSettingsFragment
        implements Indexable, WifiPickerTracker.WifiPickerTrackerCallback,
        WifiDialog2.WifiDialog2Listener, DialogInterface.OnDismissListener {

    private static final String TAG = "NetworkProviderSettings";
    public static final String ACTION_NETWORK_PROVIDER_SETTINGS =
            "android.settings.NETWORK_PROVIDER_SETTINGS";

    private static final String TAG = "NetworkProviderSettings";
    // IDs of context menu
    static final int MENU_ID_CONNECT = Menu.FIRST + 1;
    @VisibleForTesting
+204 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.network;


import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;

import static com.android.settings.slices.CustomSliceRegistry.PROVIDER_MODEL_SLICE_URI;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SubscriptionManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.slice.Slice;
import androidx.slice.builders.ListBuilder;

import com.android.settings.R;
import com.android.settings.SubSettings;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settings.network.telephony.NetworkProviderWorker;
import com.android.settings.slices.CustomSliceable;
import com.android.settings.slices.SliceBackgroundWorker;
import com.android.settings.slices.SliceBuilderUtils;
import com.android.settings.wifi.slice.WifiSlice;
import com.android.settings.wifi.slice.WifiSliceItem;
import com.android.wifitrackerlib.WifiEntry;

import java.util.List;
import java.util.stream.Collectors;

/**
 * {@link CustomSliceable} for Wi-Fi and mobile data connection, used by generic clients.
 */
// ToDo If the provider model become default design in the future, the code needs to refactor
// the whole structure and use new "data object", and then split provider model out of old design.
public class ProviderModelSlice extends WifiSlice {

    private static final String TAG = "ProviderModelSlice";
    private final ProviderModelSliceHelper mHelper;

    public ProviderModelSlice(Context context) {
        super(context);
        mHelper = getHelper();
    }

    @Override
    public Uri getUri() {
        return PROVIDER_MODEL_SLICE_URI;
    }

    private static void log(String s) {
        Log.d(TAG, s);
    }

    protected boolean isApRowCollapsed() {
        return false;
    }

    @Override
    public Slice getSlice() {
        // The provider model slice step:
        // First section:  Add a Wi-Fi item which state is connected.
        // Second section:  Add a carrier item.
        // Third section:  Add the Wi-Fi items which are not connected.
        // Fourth section:  If device has connection problem, this row show the message for user.

        if (mHelper.isAirplaneModeEnabled()) {
            log("Airplane mode is enabled.");
            // ToDo Next CL will add the Airplane mode Message.
            return mHelper.createListBuilder(getUri()).build();
        }

        int maxListSize = 0;
        List<WifiSliceItem> wifiList = null;
        final NetworkProviderWorker worker = getWorker();
        if (worker != null) {
            // get Wi-Fi list.
            wifiList = worker.getResults();
            maxListSize = worker.getApRowCount();
        } else {
            log("network provider worker is null.");
        }

        final boolean hasCarrier = mHelper.hasCarrier();
        log("hasCarrier: " + hasCarrier);


        final ListBuilder listBuilder = mHelper.createListBuilder(getUri());

        // First section:  Add a Wi-Fi item which state is connected.
        final WifiSliceItem connectedWifiItem = mHelper.getConnectedWifiItem(wifiList);
        if (connectedWifiItem != null) {
            log("get Wi-Fi item witch is connected");
            listBuilder.addRow(getWifiSliceItemRow(connectedWifiItem));
            maxListSize--;
        }

        // Second section:  Add a carrier item.
        if (hasCarrier) {
            listBuilder.addRow(mHelper.createCarrierRow());
            maxListSize--;
        }

        // Third section:  Add the Wi-Fi items which are not connected.
        if (wifiList != null) {
            log("get Wi-Fi items which are not connected");
            final List<WifiSliceItem> disconnectedWifiList = wifiList.stream()
                    .filter(wifiSliceItem -> wifiSliceItem.getConnectedState()
                            != WifiEntry.CONNECTED_STATE_CONNECTED)
                    .limit(maxListSize)
                    .collect(Collectors.toList());
            for (WifiSliceItem item : disconnectedWifiList) {
                listBuilder.addRow(getWifiSliceItemRow(item));
            }
        }

        // Fourth section:  If device has connection problem, this row show the message for user.
        // 1) show non_carrier_network_unavailable:
        //    - while no wifi item
        // 2) show all_network_unavailable:
        //    - while no wifi item + no carrier
        //    - while no wifi item + no data capability
        if (worker == null || wifiList == null) {
            log("wifiList is null");
            int resId = R.string.non_carrier_network_unavailable;
            if (!hasCarrier || mHelper.isNoCarrierData()) {
                log("No carrier item or no carrier data.");
                resId = R.string.all_network_unavailable;
            }

            if (!hasCarrier) {
                // If there is no item in ProviderModelItem, slice needs a header.
                listBuilder.setHeader(mHelper.createHeader());
            }
            listBuilder.addGridRow(mHelper.createMessageGridRow(resId));
        }

        return listBuilder.build();
    }

    /**
     * Update the current carrier's mobile data status.
     */
    @Override
    public void onNotifyChange(Intent intent) {
        final SubscriptionManager subscriptionManager = mHelper.getSubscriptionManager();
        if (subscriptionManager == null) {
            return;
        }
        final boolean newState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE,
                mHelper.isMobileDataEnabled());
        final int defaultSubId = subscriptionManager.getDefaultDataSubscriptionId();
        log("defaultSubId:" + defaultSubId);
        if (!SubscriptionManager.isUsableSubscriptionId(defaultSubId)) {
            return; // No subscription - do nothing.
        }

        MobileNetworkUtils.setMobileDataEnabled(mContext, defaultSubId, newState,
                false /* disableOtherSubscriptions */);
    }

    @Override
    public Intent getIntent() {
        final String screenTitle = mContext.getText(R.string.provider_internet_settings).toString();
        return SliceBuilderUtils.buildSearchResultPageIntent(mContext,
                NetworkProviderSettings.class.getName(), "" /* key */, screenTitle,
                SettingsEnums.SLICE)
                .setClassName(mContext.getPackageName(), SubSettings.class.getName())
                .setData(getUri());
    }

    @Override
    public Class getBackgroundWorkerClass() {
        return NetworkProviderWorker.class;
    }

    @VisibleForTesting
    ProviderModelSliceHelper getHelper() {
        return new ProviderModelSliceHelper(mContext, this);
    }

    @VisibleForTesting
    NetworkProviderWorker getWorker() {
        return SliceBackgroundWorker.getInstance(getUri());
    }
}
Loading