Back to project page network-checker.
The source code is released under:
Apache License
If you think the Android project network-checker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package de.ub0r.android.networkchecker; /* w w w. ja v a2 s . com*/ import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.SystemClock; import android.preference.PreferenceManager; import android.provider.Settings; import android.support.v4.app.NotificationCompat; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; /** * @author flx */ public class CheckReceiver extends BroadcastReceiver { private static final String TAG = "CheckReceiver"; private static final String ACTION_NOTFY = "de.ub0r.android.networkchecker.NOTIFY"; static final String ACTION_SNOOZE = "de.ub0r.android.networkchecker.SNOOZE"; private static final long DELAY_FIRST_CHECK = 10 * 1000; private static final long DELAY_NEXT_CHECK = 60 * 1000; private static final long DELAY_SNOOZE = 10 * 60 * 1000; private static final int ARGB = 0xffff; private static final int MSON = 2000; private static final int MSOFF = 1000; private static final long[] VIBRATE = new long[]{2000, 1000, 500, 1000, 500}; @Override public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "onReceive(" + intent.getAction() + ")"); if (ACTION_NOTFY.equals(intent.getAction())) { checkAndNotify(context, intent); } else if (ACTION_SNOOZE.equals(intent.getAction())) { snooze(context); } else { schedCheck(context, intent); } } private void snooze(Context context) { SharedPreferences.Editor e = PreferenceManager.getDefaultSharedPreferences(context).edit(); e.putLong("snooze_until", System.currentTimeMillis() + DELAY_SNOOZE); e.commit(); } private long getDelay(final Context context, final Intent intent) { if (ACTION_NOTFY.equals(intent.getAction())) { String s = PreferenceManager.getDefaultSharedPreferences(context) .getString("delay", "60"); long delay; try { delay = Long.parseLong(s.trim()) * 1000; } catch (NumberFormatException e) { Log.w(TAG, "not a number: " + s); delay = DELAY_NEXT_CHECK; } return delay; } else { return DELAY_FIRST_CHECK; } } private void schedCheck(final Context context, final Intent intent) { Log.d(TAG, "schedCheck()"); final PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_NOTFY, null, context, CheckReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT); final long t = SystemClock.elapsedRealtime() + getDelay(context, intent); final AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.ELAPSED_REALTIME, t, pi); } private void checkAndNotify(final Context context, final Intent intent) { Log.d(TAG, "checkAndNotify()"); if (check(context)) { notify(context, intent); } else { cancelNotification(context); } } private boolean isAirplaneModeOn(final Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { return Settings.System .getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } else { //noinspection deprecation return Settings.System .getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0; } } private boolean check(final Context context) { Log.d(TAG, "check()"); if (isAirplaneModeOn(context)) { Log.d(TAG, "airplane mode, ignore"); // ignore if, airplane more is on return false; } // telephony TelephonyManager tp = (TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE); if (Build.DEVICE.startsWith("GT-I8160")) { if (tp.getSimState() == TelephonyManager.SIM_STATE_READY && !TextUtils .isEmpty(tp.getSimOperatorName()) && TextUtils .isEmpty(tp.getNetworkOperatorName())) { Log.w(TAG, "dead phone!"); return true; } } else { if (TextUtils.isEmpty(tp.getNetworkOperator()) && TextUtils .isEmpty(tp.getNetworkOperatorName()) && tp.getNetworkType() == TelephonyManager.NETWORK_TYPE_UNKNOWN) { Log.w(TAG, "dead phone!"); return true; } } logDeviceState(tp); return false; } private void logDeviceState(final TelephonyManager tp) { // if none of the above match on a dead phone, try to find a pattern from these logs Log.d(TAG, "device: " + Build.DEVICE); Log.d(TAG, "net operator: " + tp.getNetworkOperator()); Log.d(TAG, "net operator name: " + tp.getNetworkOperatorName()); Log.d(TAG, "net type: " + tp.getNetworkType()); Log.d(TAG, "sim operator: " + tp.getSimOperator()); Log.d(TAG, "sim operator name: " + tp.getSimOperatorName()); Log.d(TAG, "sim state: " + tp.getSimState()); Log.d(TAG, "sim sn: " + tp.getSimSerialNumber()); } private void notify(final Context context, final Intent intent) { Log.d(TAG, "notify()"); SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context); if (!p.getBoolean("enable", true)) { Log.i(TAG, "notifications are disabled"); return; } if (System.currentTimeMillis() > p.getLong("snooze_until", 0)) { NotificationCompat.Builder b = new NotificationCompat.Builder(context); b.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, InfoActivity.class), PendingIntent.FLAG_CANCEL_CURRENT)); b.setSmallIcon(android.R.drawable.stat_sys_warning); b.setContentText(context.getString(R.string.notification_message)); b.setContentTitle(context.getString(R.string.notification_title)); b.addAction(0, context.getString(R.string.snooze), PendingIntent .getBroadcast(context, 0, new Intent(ACTION_SNOOZE, null, context, CheckReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT)); b.setLights(ARGB, MSON, MSOFF); String s = p.getString("sound", null); if (s != null) { b.setSound(Uri.parse(s)); } if (p.getBoolean("vibrate", true)) { b.setVibrate(VIBRATE); } NotificationManager nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(0, b.build()); } else { Log.i(TAG, "snoozing"); } // schedule next notification schedCheck(context, intent); } private void cancelNotification(final Context context) { Log.d(TAG, "cancelNotification()"); NotificationManager nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); nm.cancelAll(); } }