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

Commit 70ecea22 authored by Jason Monk's avatar Jason Monk
Browse files

Add cellular data off conditional

Change-Id: I34980f3fc09bf5dd47eb296c5b1c5fccc87b5c90
parent af24b637
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -2692,5 +2692,13 @@
            </intent-filter>
       </receiver>

        <receiver
            android:name=".dashboard.conditional.CellularDataCondition$Receiver"
            android:enabled="false">
            <intent-filter>
                 <action android:name="android.intent.action.ANY_DATA_STATE" />
            </intent-filter>
       </receiver>

    </application>
</manifest>
+41 −0
Original line number Diff line number Diff line
<!--
Copyright (C) 2014 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:autoMirrored="true"
        android:width="17dp"
        android:height="17dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M8,9
        l-2.87,-2.87
        -2.87,2.87
        -1.13,-1.13
        2.87,-2.87
        -2.87,-2.87
        1.13,-1.13
        2.87,2.87
        2.87,-2.87
        1.13,1.13,
        -2.87,2.87
        2.87,2.87
        L8,9
        z" />
    <path
        android:fillColor="@android:color/white"
        android:pathData="M2.000000,22.000000l20.000000,0.000000 0.000000,-20.000000z"/>
</vector>
+9 −0
Original line number Diff line number Diff line
@@ -6693,6 +6693,9 @@
    <!-- Turn off a conditional state of the device (e.g. airplane mode, or hotspot) [CHAR LIMIT=30] -->
    <string name="condition_turn_off">Turn off</string>

    <!-- Turn on a conditional state of the device (e.g. cellular data) [CHAR LIMIT=30] -->
    <string name="condition_turn_on">Turn on</string>

    <!-- Title of condition that hotspot is on [CHAR LIMIT=30] -->
    <string name="condition_hotspot_title">Hotspot is on</string>

@@ -6714,4 +6717,10 @@
    <!-- Summary of condition that battery saver is on [CHAR LIMIT=NONE] -->
    <string name="condition_battery_summary">Performance is reduced. Location services and background data are turned off.</string>

    <!-- Title of condition that cellular data is off [CHAR LIMIT=30] -->
    <string name="condition_cellular_title">Cellular data is off</string>

    <!-- Summary of condition that cellular data is off [CHAR LIMIT=NONE] -->
    <string name="condition_cellular_summary">Internet is available only via Wi-Fi</string>

</resources>
+2 −9
Original line number Diff line number Diff line
@@ -16,10 +16,8 @@
package com.android.settings.dashboard.conditional;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.net.ConnectivityManager;
import com.android.settings.R;
@@ -38,13 +36,8 @@ public class AirplaneModeCondition extends Condition {
    }

    @Override
    protected void onSilenceChanged(boolean silenced) {
        // Only need to listen for airplane mode changes when its been silenced.
        PackageManager pm = mManager.getContext().getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), Receiver.class),
                silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    protected Class<?> getReceiverClass() {
        return Receiver.class;
    }

    @Override
+95 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.dashboard.conditional;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.TelephonyIntents;
import com.android.settings.R;
import com.android.settings.Settings;

public class CellularDataCondition extends Condition {

    public CellularDataCondition(ConditionManager manager) {
        super(manager);
    }

    @Override
    public void refreshState() {
        ConnectivityManager connectivity = mManager.getContext().getSystemService(
                ConnectivityManager.class);
        TelephonyManager telephony = mManager.getContext().getSystemService(TelephonyManager.class);
        if (!connectivity.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)
                || telephony.getSimState() != TelephonyManager.SIM_STATE_READY) {
            setActive(false);
            return;
        }
        setActive(!telephony.getDataEnabled());
    }

    @Override
    protected Class<?> getReceiverClass() {
        return Receiver.class;
    }

    @Override
    public Icon getIcon() {
        return Icon.createWithResource(mManager.getContext(), R.drawable.ic_cellular_off);
    }

    @Override
    public CharSequence getTitle() {
        return mManager.getContext().getString(R.string.condition_cellular_title);
    }

    @Override
    public CharSequence getSummary() {
        return mManager.getContext().getString(R.string.condition_cellular_summary);
    }

    @Override
    public CharSequence[] getActions() {
        return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_on) };
    }

    @Override
    public void onPrimaryClick() {
        mManager.getContext().startActivity(new Intent(mManager.getContext(),
                Settings.DataUsageSummaryActivity.class));
    }

    @Override
    public void onActionClick(int index) {
        if (index == 0) {
            TelephonyManager telephony = mManager.getContext().getSystemService(
                    TelephonyManager.class);
            telephony.setDataEnabled(true);
            setActive(false);
        } else {
            throw new IllegalArgumentException("Unexpected index " + index);
        }
    }

    public static class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED.equals(
                    intent.getAction())) {
                ConditionManager.get(context).getCondition(CellularDataCondition.class)
                        .refreshState();
            }
        }
    }
}
Loading