package com.android.settings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.util.Log;
import android.os.SystemProperties;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.ArrayList;
public class BootReceiver extends BroadcastReceiver{
private final boolean DEBUG = false;
@Override
public void onReceive(Context mContext, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
SharedPreferences shared = mContext.getSharedPreferences("com.android.settings_preferences", Context.MODE_PRIVATE);
boolean enable_log_save = shared.getBoolean("enable_log_save", false);
String persist_sys_cat_log =SystemProperties.get("persist.sys.cat_log");
if (DEBUG) Log.d("BootReceiver","action==ACTION_BOOT_COMPLETED,BootReceiver is start");
if(persist_sys_cat_log.equals("1")){
SystemProperties.set("persist.sys.cat_log","1");
}
}else if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
if(SystemProperties.getBoolean("persist.tethering.tether_eth", false)){
if (DEBUG) Log.d("BootReceiver", "以太网状态变化");
ConnectivityManager cm =
(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null&&(networkInfo.getType() == ConnectivityManager.TYPE_WIFI || networkInfo.getType() == ConnectivityManager.TYPE_MOBILE))
{
if (DEBUG) Log.d("BootReceiver", "以太网状态变化设置");
ArrayList<String> available = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_AVAILABLE_TETHER);
ArrayList<String> active = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_ACTIVE_TETHER);
ArrayList<String> errored = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_ERRORED_TETHER);
updateEthernetState(mContext,available.toArray(new String[available.size()]),
active.toArray(new String[active.size()]),
errored.toArray(new String[errored.size()]));
}
}
}else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
if (DEBUG) Log.d("BootReceiver", "网络状态变化");
if(SystemProperties.getBoolean("persist.tethering.tether_eth", false)){
ConnectivityManager cm =
(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null&&(networkInfo.getType() == ConnectivityManager.TYPE_WIFI || networkInfo.getType() == ConnectivityManager.TYPE_MOBILE))
{
if (DEBUG) Log.d("BootReceiver", "网络状态变化设置");
String[] available = cm.getTetherableIfaces();
String[] tethered = cm.getTetheredIfaces();
String[] errored = cm.getTetheringErroredIfaces();
updateEthernetState(mContext,available,tethered,errored);
}
}
}
}
private void updateEthernetState(Context mContext,String[] available, String[] tethered,
String[] errored) {
ConnectivityManager cm =
(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
String[] mEthRegexs = cm.getTetherableEthRegexs();
int ethError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
for (String s : available) {
for (String regex : mEthRegexs) {
if (s.matches(regex)) {
if (ethError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
ethError = cm.getLastTetherError(s);
}
}
}
}
boolean ethTethered = false;
for (String s : tethered) {
for (String regex : mEthRegexs) {
if (s.matches(regex)) ethTethered = true;
}
}
boolean ethErrored = false;
for (String s: errored) {
for (String regex : mEthRegexs) {
if (s.matches(regex)) ethErrored = true;
}
}
if (ethTethered) {
SystemProperties.set("persist.tethering.tether_eth", "true");
if (DEBUG) Log.d("BootReceiver", "以太网分享成功");
} else if (ethError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
if (SystemProperties.getBoolean("persist.tethering.tether_eth", false)) {
Log.d("BootReceiver", "Should auto turn on terthering");
cm.setEthTethering(true);
}
}
}
}
- 1
- 2
前往页