Created
January 29, 2014 19:44
-
-
Save snadjafi/8695463 to your computer and use it in GitHub Desktop.
in Activity BroadcastReceiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.kuapay.mo.activity; | |
| import android.app.ActionBar; | |
| import android.app.AlertDialog; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.DialogInterface; | |
| import android.content.Intent; | |
| import android.content.IntentFilter; | |
| import android.net.ConnectivityManager; | |
| import android.net.NetworkInfo; | |
| import android.os.BatteryManager; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.webkit.WebView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import com.kuapay.mo.KuaApplication; | |
| import com.kuapay.mo.fragment.MobileOrderingWebViewFragment; | |
| import com.kuapay.mo.R; | |
| import com.kuapay.mo.fragment.PinDialogFragment; | |
| import com.kuapay.mo.interfaces.PinDialogFragmentInterface; | |
| import com.kuapay.mo.util.Utils; | |
| public class MainActivity extends BaseFragmentActivity implements PinDialogFragmentInterface | |
| { | |
| //region Constant | |
| private final String DEBUG_TAG = "["+getClass().getSimpleName()+"]"; | |
| private static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; | |
| private static final String BATTERY_ACTION = "android.intent.action.ACTION_BATTERY_LOW"; | |
| private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; | |
| private static final String WEB_VIEW_FRAGMENT = "web_view_fragment"; | |
| private static final String PIN_DIALOG_FRAGMENT = "pin_dialog_fragment"; | |
| //endregion | |
| //region Variables | |
| private AlertDialog batteryAlertDialog; | |
| private AlertDialog connectivityAlertDialog; | |
| private ConnectivityReceiver connectivityReceiver; | |
| private BatteryLevelReceiver batteryLevelReceiver; | |
| private BootReceiver bootReceiver; | |
| private TextView darkOverylayTextView; | |
| //endregion | |
| //region Listeners | |
| private View.OnClickListener hideActionBarClickListener = new View.OnClickListener() | |
| { | |
| @Override | |
| public void onClick(View view) | |
| { | |
| ActionBar mActionBar = getActionBar(); | |
| if(mActionBar.isShowing()) | |
| mActionBar.hide(); | |
| darkOverylayTextView.setVisibility(View.GONE); | |
| } | |
| }; | |
| //endregion | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) | |
| { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| ActionBar mActionBar = getActionBar(); | |
| mActionBar.hide(); | |
| if (savedInstanceState == null) | |
| getFragmentManager() | |
| .beginTransaction() | |
| .add(R.id.fragment_placeHolder_frameLayout,new MobileOrderingWebViewFragment(), WEB_VIEW_FRAGMENT) | |
| .commit(); | |
| connectivityReceiver = new ConnectivityReceiver(); | |
| batteryLevelReceiver = new BatteryLevelReceiver(); | |
| bootReceiver = new BootReceiver(); | |
| IntentFilter filter = new IntentFilter(CONNECTIVITY_ACTION); | |
| registerReceiver(connectivityReceiver,filter); | |
| filter = new IntentFilter(BATTERY_ACTION); | |
| registerReceiver(batteryLevelReceiver,filter); | |
| filter = new IntentFilter(BOOT_COMPLETED); | |
| registerReceiver(bootReceiver, filter); | |
| bindUIElements(); | |
| setListeners(); | |
| } | |
| @Override | |
| public void onResume() | |
| { | |
| super.onResume(); | |
| if (((KuaApplication)getApplication()).isNewURL()) | |
| { | |
| ((KuaApplication) getApplication()).setHasNewURL(false); | |
| reloadWithNewURL(); | |
| } | |
| } | |
| @Override | |
| public void reloadWithNewURL() | |
| { | |
| getFragmentManager() | |
| .beginTransaction() | |
| .replace(R.id.fragment_placeHolder_frameLayout,new MobileOrderingWebViewFragment(), WEB_VIEW_FRAGMENT) | |
| .commit(); | |
| } | |
| @Override | |
| protected void onDestroy() | |
| { | |
| unregisterReceiver(connectivityReceiver); | |
| unregisterReceiver(batteryLevelReceiver); | |
| unregisterReceiver(bootReceiver); | |
| super.onDestroy(); | |
| } | |
| @Override | |
| public void onBackPressed() | |
| { | |
| MobileOrderingWebViewFragment fragment = (MobileOrderingWebViewFragment)getFragmentManager().findFragmentByTag(WEB_VIEW_FRAGMENT); | |
| if (fragment.isVisible()) | |
| { | |
| WebView webView = fragment.getWebView(); | |
| if(webView != null && webView.canGoBack()) | |
| webView.goBack(); | |
| } | |
| PinDialogFragment selectDialogFragment = new PinDialogFragment(); | |
| selectDialogFragment.show(getFragmentManager(), PIN_DIALOG_FRAGMENT); | |
| } | |
| private void bindUIElements() | |
| { | |
| darkOverylayTextView = (TextView) findViewById(R.id.dark_overlay_frameLayout); | |
| } | |
| private void setListeners() | |
| { | |
| darkOverylayTextView.setOnClickListener(hideActionBarClickListener); | |
| } | |
| private void showNetworkDialog() | |
| { | |
| if (connectivityAlertDialog == null) | |
| { | |
| AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialogTheme); | |
| builder.setTitle(getString(R.string.header_connectivity_issue)) | |
| .setMessage(getString(R.string.body_connect_to_internet)) | |
| .setCancelable(false) | |
| .setNeutralButton(getString(R.string.ok), new DialogInterface.OnClickListener() | |
| { | |
| public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) | |
| { | |
| dialog.cancel(); | |
| } | |
| }); | |
| connectivityAlertDialog = builder.create(); | |
| connectivityAlertDialog.show(); | |
| } | |
| else if (!connectivityAlertDialog.isShowing()) | |
| connectivityAlertDialog.show(); | |
| } | |
| private void showBatteryDialog() | |
| { | |
| if (batteryAlertDialog == null) | |
| { | |
| AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialogTheme); | |
| builder.setTitle(getString(R.string.header_battery_low)) | |
| .setMessage(getString(R.string.body_connect_power)) | |
| .setCancelable(false) | |
| .setNeutralButton(getString(R.string.ok), new DialogInterface.OnClickListener() | |
| { | |
| public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) | |
| { | |
| dialog.cancel(); | |
| } | |
| }); | |
| batteryAlertDialog = builder.create(); | |
| batteryAlertDialog.show(); | |
| } | |
| else if (!batteryAlertDialog.isShowing()) | |
| batteryAlertDialog.show(); | |
| } | |
| @Override | |
| public void onVerifyPin(String pin) | |
| { | |
| PinDialogFragment pinDialogFragment = (PinDialogFragment) getFragmentManager().findFragmentByTag(PIN_DIALOG_FRAGMENT); | |
| if(pin.equalsIgnoreCase(getString(R.string.pin))) | |
| { | |
| if(pinDialogFragment != null) | |
| pinDialogFragment.dismiss(); | |
| ActionBar mActionBar = getActionBar(); | |
| if(!mActionBar.isShowing()) | |
| { mActionBar.show(); | |
| darkOverylayTextView.setVisibility(View.VISIBLE); | |
| } | |
| Utils.hideKeyboard(this); | |
| } | |
| else | |
| { | |
| Toast.makeText(this, getString(R.string.msg_error_wrong_pin), Toast.LENGTH_SHORT).show(); | |
| if(pinDialogFragment != null) | |
| pinDialogFragment.clearPin(); | |
| } | |
| } | |
| //region Classes | |
| private class ConnectivityReceiver extends BroadcastReceiver | |
| { | |
| public void onReceive(Context context, Intent intent) | |
| { | |
| ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
| NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
| boolean isConnected = netInfo != null && netInfo.isConnectedOrConnecting(); | |
| Log.i(DEBUG_TAG, isConnected ? "is connected" : "not connected"); | |
| if (!isConnected) | |
| showNetworkDialog(); | |
| } | |
| } | |
| private class BatteryLevelReceiver extends BroadcastReceiver | |
| { | |
| public void onReceive(Context context, Intent intent) | |
| { | |
| int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); | |
| boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||status == BatteryManager.BATTERY_STATUS_FULL; | |
| if (!isCharging) | |
| showBatteryDialog(); | |
| } | |
| } | |
| private class BootReceiver extends BroadcastReceiver | |
| { | |
| public void onReceive(Context context, Intent intent) | |
| { | |
| reloadWithNewURL(); | |
| } | |
| } | |
| //endregion | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment