mobi.cangol.mobile.service.status.StatusServiceImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2013 Cangol
*
* 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 mobi.cangol.mobile.service.status;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import java.util.ArrayList;
import mobi.cangol.mobile.logging.Log;
import mobi.cangol.mobile.service.Service;
import mobi.cangol.mobile.service.ServiceProperty;
import mobi.cangol.mobile.utils.DeviceInfo;
/**
* @author Cangol
*/
@Service("StatusService")
class StatusServiceImpl implements StatusService {
private final static String TAG = "StatusService";
protected ArrayList listeners = new ArrayList();
private boolean debug = false;
private Application mContext = null;
private TelephonyManager mTelephonyManager;
private boolean mCallingState = true;
private ServiceProperty mServiceProperty = null;
private BroadcastReceiver networkStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
State wifiState = null;
State mobileState = null;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfoWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfoWifi != null) {
wifiState = networkInfoWifi.getState();
}
NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkInfo != null) {
mobileState = networkInfo.getState();
}
if (wifiState != null && mobileState != null && State.CONNECTED != wifiState
&& State.CONNECTED == mobileState) {
// 手机网络连接成功
Log.d(TAG, "手机网络连接成功 ");
if (listeners.size() > 0) {
notifyNetworkTo3G(context);
}
} else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState
&& State.CONNECTED != mobileState) {
// 手机没有任何的网络
Log.d(TAG, "手机没有任何的网络,网络中断 ");
if (listeners.size() > 0) {
notifyNetworkDisconnect(context);
}
} else if (wifiState != null && State.CONNECTED == wifiState) {
// 无线网络连接成功
Log.d(TAG, " 无线网络连接成功");
if (listeners.size() > 0) {
notifyNetworkConnect(context);
}
}
}
};
/**
* ACTION_MEDIA_MOUNTED 扩展介质被插入,而且已经被挂载。 ACTION_MEDIA_UNMOUNTED
* 扩展介质存在,但是还没有被挂载 (mount)。
*
* ACTION_MEDIA_EJECT 用户想要移除扩展介质(拔掉扩展卡)。 ACTION_MEDIA_SHARED 扩展介质的挂载被解除
* (unmount),因为它已经作为 USB 大容量存储被共享。 ACTION_MEDIA_BAD_REMOVAL 扩展介质(扩展卡)已经从 SD
* 卡插槽拔出,但是挂载点 (mount point) 还没解除 (unmount)。 ACTION_MEDIA_REMOVED 扩展介质被移除。
*/
private BroadcastReceiver storageStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == Intent.ACTION_MEDIA_EJECT) {
} else if (intent.getAction() == Intent.ACTION_MEDIA_SHARED) {
} else if (intent.getAction() == Intent.ACTION_MEDIA_BAD_REMOVAL) {
} else if (intent.getAction() == Intent.ACTION_MEDIA_REMOVED) {
if (listeners.size() > 0) {
notifyStorageRemove(context);
}
} else if (intent.getAction() == Intent.ACTION_MEDIA_MOUNTED) {
if (listeners.size() > 0) {
notifyStorageMount(context);
} else {
//
}
}
}
};
private PhoneStateListener phoneStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// 闲置 挂起
Log.d(TAG, "CALL_STATE_IDLE");
mCallingState = false;
if (listeners.size() > 0) {
notifyCallStateIdle();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// 摘机
Log.d(TAG, "CALL_STATE_OFFHOOK");
mCallingState = true;
if (listeners.size() > 0) {
notifyCallStateOffhook();
}
break;
case TelephonyManager.CALL_STATE_RINGING:
// 响铃
Log.d(TAG, "CALL_STATE_RINGING");
mCallingState = true;
if (listeners.size() > 0) {
notifyCallStateRinging();
}
break;
}
}
};
@Override
public void onCreate(Application context) {
mContext = context;
IntentFilter intentFileter1 = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
mContext.registerReceiver(networkStatusReceiver, intentFileter1);
IntentFilter intentFileter2 = new IntentFilter("android.intent.action.ACTION_MEDIA_MOUNTED");
intentFileter2.addAction("android.intent.action.ACTION_MEDIA_REMOVED");
mContext.registerReceiver(storageStatusReceiver, intentFileter2);
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public void init(ServiceProperty serviceProperty) {
this.mServiceProperty = serviceProperty;
}
@Override
public ServiceProperty defaultServiceProperty() {
ServiceProperty sp = new ServiceProperty(TAG);
return sp;
}
@Override
public void onDestroy() {
mContext.unregisterReceiver(networkStatusReceiver);
mContext.unregisterReceiver(storageStatusReceiver);
mTelephonyManager.listen(null, PhoneStateListener.LISTEN_NONE);
}
@Override
public String getName() {
return TAG;
}
@Override
public void setDebug(boolean debug) {
this.debug = debug;
}
@Override
public ServiceProperty getServiceProperty() {
return mServiceProperty;
}
@Override
public boolean isConnection() {
return DeviceInfo.isConnection(mContext);
}
@Override
public boolean isWifiConnection() {
return DeviceInfo.isWifiConnection(mContext);
}
@Override
public boolean isGPSLocation() {
return DeviceInfo.isGPSLocation(mContext);
}
@Override
public boolean isNetworkLocation() {
return DeviceInfo.isNetworkLocation(mContext);
}
@Override
public void registerStatusListener(StatusListener statusListener) {
if (statusListener == null) {
throw new IllegalArgumentException("The StatusListener is null.");
}
synchronized (listeners) {
if (listeners.contains(statusListener)) {
throw new IllegalStateException("StatusListener " + statusListener + " is already registered.");
}
listeners.add(statusListener);
}
}
@Override
public void unregisterStatusListener(StatusListener statusListener) {
if (statusListener == null) {
throw new IllegalArgumentException("The StatusListener is null.");
}
synchronized (listeners) {
if (listeners.contains(statusListener)) {
listeners.remove(statusListener);
} else {
throw new IllegalStateException("StatusListener " + statusListener + " is not exist.");
}
}
}
private void notifyNetworkConnect(Context context) {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.networkConnect(context);
}
}
}
private void notifyNetworkDisconnect(Context context) {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.networkDisconnect(context);
} else {
Log.e("null=" + listener);
}
}
}
private void notifyNetworkTo3G(Context context) {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.networkTo3G(context);
}
}
}
private void notifyStorageRemove(Context context) {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.storageRemove(context);
}
}
}
private void notifyStorageMount(Context context) {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.storageMount(context);
}
}
}
private void notifyCallStateIdle() {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.callStateIdle();
}
}
}
private void notifyCallStateOffhook() {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.callStateOffhook();
}
}
}
private void notifyCallStateRinging() {
for (StatusListener listener : listeners) {
if (listener != null) {
listener.callStateRinging();
}
}
}
@Override
public boolean isCallingState() {
return mCallingState;
}
}