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

Commit 2799b392 authored by Prashant Shrivastava's avatar Prashant Shrivastava Committed by Linux Build Service Account
Browse files

frameworks/base: Addition of Changes for ZeroBalance feature

--Addition of code to receive broadcast from ZeroBalanceHelper
  application for executing command to write block/unblock
  rule to Iptable
--Addition of ZeroBalanceHelper file to be accesible by other
   modules to notify ZeroBalanceHelper application about update
   on change of SIM balance
--Addition of rule to allow browser uid when rest all applications
  are in blocked state.
--Addition of feature flag to control the zerobalance feature.

Change-Id: Idb47f46e6e86397e582c2fda1dbe7dd9d5e1624e
CRs-Fixed: 1033837
parent 8ddb32bd
Loading
Loading
Loading
Loading
+100 −0
Original line number Diff line number Diff line
/*
 ** Copyright (c) 2015-16, The Linux Foundation. All rights reserved.

 ** Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **     * Redistributions of source code must retain the above copyright
 **       notice, this list of conditions and the following disclaimer.
 **     * Redistributions in binary form must reproduce the above
 **       copyright notice, this list of conditions and the following
 **       disclaimer in the documentation and/or other materials provided
 **       with the distribution.
 **     * Neither the name of The Linux Foundation nor the names of its
 **       contributors may be used to endorse or promote products derived
 **       from this software without specific prior written permission.

 ** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package android.net;

import android.app.ActivityThread;
import android.content.Context;
import android.content.Intent;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.util.Log;

import com.android.internal.R;


/** @hide */
public final class ZeroBalanceHelper {

    public static final String BACKGROUND_DATA_PROPERTY = "sys.background.data.disable";
    public static final String BACKGROUND_DATA_BROADCAST = "org.codeaurora.background.data";
    public static final String TAG = "ZeroBalance";

    private static int sRedirectCount = 0;
    private static int sRedirectMaxCount = 3;

    private Context mContext = null;

    public ZeroBalanceHelper() {
        mContext = ActivityThread.currentApplication();
    }

    public void setBgDataProperty(String enabled) {
        Intent intent = new Intent();
        intent.setAction(BACKGROUND_DATA_BROADCAST);
        intent.putExtra("enabled", enabled);
        mContext.sendBroadcast(intent);
    }

    public String getBgDataProperty() {
        String isBgDataPropertySet = SystemProperties.get(BACKGROUND_DATA_PROPERTY, "false");
        if(Boolean.valueOf(isBgDataPropertySet)) {
            sRedirectCount = 0;
        }
        return isBgDataPropertySet;
    }

    private String getConfiguredRedirectURL() {
        String redirectURL = mContext.getResources().getString(
                com.android.internal.R.string.operator_config_url);
        Log.d(TAG, "Returning the configured redirect URL   :   "
                + redirectURL);
        return redirectURL;
    }

    public synchronized void setHttpRedirectCount(String url) {
        String redirectUrl = getConfiguredRedirectURL();
        if (redirectUrl != null && url.contains(redirectUrl)) {
            sRedirectCount++;
            Log.d(TAG, "http:sRedirectCount="+sRedirectCount);
            if (sRedirectCount >= sRedirectMaxCount) {
                Log.d(TAG,"http:Background Data will be disabled" );
                setBgDataProperty("true");
                sRedirectCount = 0;
            }
        } else {
            Log.d(TAG,"http: resetting the counter ");
            sRedirectCount = 0;
        }
    }

    public boolean getFeatureConfigValue() {
        return mContext.getResources().getBoolean(R.bool.config_zero_balance_operator);
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -2579,4 +2579,11 @@
    <string name="config_rat_2g" translatable="false">2G</string>
    <string name="config_rat_3g" translatable="false">3G</string>
    <string name="config_rat_4g" translatable="false">4G</string>

    <!-- Zero Balance redirect URL config -->
    <string name="operator_config_url" translatable="false"></string>
    <!-- Zero Balance ping URL config -->
    <string name="operator_ping_url" translatable="false"></string>
    <!-- Zero Balance feature enable config -->
    <bool name="config_zero_balance_operator">false</bool>
</resources>
+3 −0
Original line number Diff line number Diff line
@@ -2686,4 +2686,7 @@

  <java-symbol type="array" name="origin_carrier_names" />
  <java-symbol type="array" name="locale_carrier_names" />
  <java-symbol type="string" name="operator_config_url" />
  <java-symbol type="string" name="operator_ping_url" />
  <java-symbol type="bool" name="config_zero_balance_operator"/>
</resources>
+36 −0
Original line number Diff line number Diff line
@@ -47,8 +47,11 @@ import static com.android.server.NetworkManagementService.NetdResponseCode.TtyLi
import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
import android.annotation.NonNull;
import android.app.ActivityManagerNative;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.INetd;
import android.net.INetworkManagementEventObserver;
@@ -90,6 +93,7 @@ import android.util.SparseIntArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.app.IBatteryStats;
import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.R;
import com.android.internal.util.HexDump;
import com.android.internal.util.Preconditions;
import com.android.server.NativeDaemonConnector.Command;
@@ -349,6 +353,13 @@ public class NetworkManagementService extends INetworkManagementService.Stub
        } else {
            prepareNativeDaemon();
        }
        //Registering the receiver for Zerobalance blocking/unblocking
        if (mContext.getResources().getBoolean(R.bool.config_zero_balance_operator)) {
            final IntentFilter restrictFilter = new IntentFilter();
            restrictFilter.addAction("org.codeaurora.restrictData");
            mContext.registerReceiver(mZeroBalanceReceiver, restrictFilter);
        }
        if (DBG) Slog.d(TAG, "ZeroBalance registering receiver");
    }

    private IBatteryStats getBatteryStats() {
@@ -2786,4 +2797,29 @@ public class NetworkManagementService extends INetworkManagementService.Stub
    public void removeInterfaceFromLocalNetwork(String iface) {
        modifyInterfaceInNetwork("remove", "local", iface);
    }

    private BroadcastReceiver mZeroBalanceReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isBlockAllData = false;
            if(intent != null
                    && intent.getAction().equals("org.codeaurora.restrictData")) {
                isBlockAllData = intent.getBooleanExtra("Restrict",false);
                Log.wtf("ZeroBalance", "Intent value to block unblock data"+isBlockAllData);
            }
            mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);

            // silently discard when control disabled
            // TODO: eventually migrate to be always enabled
            if (!mBandwidthControlEnabled) return;
            try {
                Log.wtf("ZeroBalance", "before calling connector Intent"
                        +"value to block unblock data"+isBlockAllData);
                mConnector.execute("bandwidth",
                        isBlockAllData ? "blockAllData" : "unblockAllData");
            } catch (NativeDaemonConnectorException e) {
                throw e.rethrowAsParcelableException();
            }
        }
    };
}