Back to project page twawm2.
The source code is released under:
Copyright (c) 2014, afnf All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistr...
If you think the Android project twawm2 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 com.appspot.afnf4199ga.twawm.app; //w ww . j a v a2 s. c o m import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.net.Uri; import android.net.wifi.WifiManager; import android.os.Bundle; import android.text.TextPaint; import android.util.DisplayMetrics; import android.view.Menu; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.MarginLayoutParams; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ToggleButton; import com.appspot.afnf4199ga.twawm.Const; import com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivity; import com.appspot.afnf4199ga.twawm.ctl.ListItem; import com.appspot.afnf4199ga.twawm.router.EcoModeControl; import com.appspot.afnf4199ga.twawm.router.RouterControl; import com.appspot.afnf4199ga.twawm.router.RouterControlByHttp; import com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.CTRL; import com.appspot.afnf4199ga.twawm.router.RouterInfo.COM_TYPE; import com.appspot.afnf4199ga.utils.AndroidUtils; import com.appspot.afnf4199ga.utils.Logger; import com.appspot.afnf4199ga.utils.MyStringUtlis; import net.afnf.and.twawm2.R; import java.util.ArrayList; public class MainActivity extends Activity { private AlertDialog dialog; private WifiManager wifi; private static boolean initWizardDisplayed = false; @Override public void onCreate(Bundle savedInstanceState) { Logger.v("MainActivity onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ???? TextPaint textPaint = ((TextView) findViewById(R.id.textNotWorksFine)).getPaint(); textPaint.setUnderlineText(true); // UI???? UIAct.init(this); // WifiManager???? wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); // ??????? setLayoutMargin(); } @Override protected void onStart() { super.onStart(); // ???????????changeStyle?????? DefaultWidgetProvider.changeStyle(this); boolean wifiEnabled = AndroidUtils.isWifiEnabled(wifi); boolean suppCompleted = wifiEnabled && BackgroundService.isSupplicantCompleted(wifi); BackgroundService service = BackgroundService.getInstance(); // ??? boolean working = Const.getPrefWorking(this); if (working) { Boolean ecoCharge = null; // ??????????????? if (service != null) { // UI?????? service.getStateMachine().reflesh(false); ecoCharge = service.getEcoCharge(); } // ???????????????????WiFi??????????????? else if (wifiEnabled) { Intent srvIntent = new Intent(this, BackgroundService.class); startService(srvIntent); } UIAct.postActivityButton(true, true, wifiEnabled, suppCompleted, ecoCharge, null, null); } // ??????? else { // UI?? updateAsWorkingOrPausing(this, false); } // toggleWorking ToggleButton btn = (ToggleButton) findViewById(R.id.toggleWorking); btn.setChecked(working); // ??????? Intent intent = getIntent(); if (intent != null) { // ???????????? if (intent.getBooleanExtra(Const.INTENT_EX_ACTION_SELECT, false)) { intent.removeExtra(Const.INTENT_EX_ACTION_SELECT); // 1???????????????????????????? showActionDialog(); } // Bluetooth???????? else if (intent.getBooleanExtra(Const.INTENT_EX_BT_ENABLING, false)) { uiactSetRouterToggleButton(false, null); uiactSetWifiToggleButton(false, null); intent.removeExtra(Const.INTENT_EX_BT_ENABLING); // 1???????????????????????????? Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, Const.REQUEST_ENABLE_BT); } // ??????? else if (intent.getBooleanExtra(Const.INTENT_EX_INIT_WIZARD, false)) { if (initWizardDisplayed == false) { // ?????????????? if (service != null) { service.terminateOnlineCheck(); } // 1???????????????????????????????????????????????????initWizardDisplayed??? intent.removeExtra(Const.INTENT_EX_INIT_WIZARD); // ?????? InitialConfigurationWizardActivity.startWizard(this); } return; } } initWizardDisplayed = false; } @Override protected void onResume() { //Logger.v("MainActivity onResume"); super.onResume(); // UI???? UIAct.init(this); } @Override protected void onPause() { //Logger.v("MainActivity onPause"); super.onPause(); // ????????????????????? if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } @Override protected void onDestroy() { // UIAct???? UIAct.destroy(); // WifiManager?? wifi = null; // ?????????????????????? BackgroundService service = BackgroundService.getInstance(); if (service != null) { // WiFi???????????????????????????? if (service.isWifiDisabledState() || Const.getPrefWorking(this) == false) { service.stopServiceImmediately(); } } super.onDestroy(); } public void onToggleRouter(View view) { uiactSetRouterToggleButton(false, null); // ????????????????????????????????????????????????????????????????????startService???? BackgroundService service = BackgroundService.getInstance(); if (service != null) { service.toggleRouterFromUI(false); } else { Intent srvIntent = new Intent(this, BackgroundService.class); srvIntent.putExtra(Const.INTENT_EX_TOGGLE_ROUTER, true); startService(srvIntent); } } public void onToggleWifi(View view) { uiactSetWifiToggleButton(false, null); // ????????????????????????????????????????????????????????????????????startService???? BackgroundService service = BackgroundService.getInstance(); if (service != null) { service.toggleWifiFromUI(); } else { Intent srvIntent = new Intent(this, BackgroundService.class); srvIntent.putExtra(Const.INTENT_EX_TOGGLE_WIFI, true); startService(srvIntent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { int i = 1; menu.add(Menu.NONE, i++, Menu.NONE, R.string.settings).setIcon(android.R.drawable.ic_menu_preferences) .setIntent(new Intent(this, MyPreferenceActivity.class)); menu.add(Menu.NONE, i++, Menu.NONE, R.string.send_log_short).setIcon(android.R.drawable.ic_menu_upload) .setIntent(new Intent(this, LogSendActivity.class)); menu.add(Menu.NONE, i++, Menu.NONE, R.string.info).setIcon(android.R.drawable.ic_menu_info_details) .setIntent(new Intent(this, InfoActivity.class)); return super.onCreateOptionsMenu(menu); } public void onWdClicked(View view) { showActionDialog(); } public void onToggleWorking(View view) { // ?????? uiactToggleWorkingToggleButton(false); // 2?????????????? UIAct.postDelayedEnableWorkingToggleButton(); // ???? ToggleButton btn = (ToggleButton) view; boolean change_to_working = btn.isChecked(); Const.updatePrefWorking(this, change_to_working); // ???????????? if (change_to_working) { // UI?? WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); boolean wifiEnabled = AndroidUtils.isWifiEnabled(wifi); boolean suppCompleted = wifiEnabled && BackgroundService.isSupplicantCompleted(wifi); updateAsWorking(wifiEnabled, suppCompleted); // WiFi??????????????? if (wifiEnabled) { Intent srvIntent = new Intent(this, BackgroundService.class); startService(srvIntent); } } // ???????????????? else { // UI?? updateAsWorkingOrPausing(this, false); // ???????? BackgroundService service = BackgroundService.getInstance(); if (service == null) { Logger.e("service is null on MainActivity.onToggleWorking(pause)"); return; } service.stopServiceImmediately(); } } public void onSettings(View view) { startActivity(new Intent(this, MyPreferenceActivity.class)); } public void onNotWorksFine(View view) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Const.URL_WIKI_NOT_WORKS)); startActivity(intent); } public void onToggleEcoCharge(View view) { ToggleButton btn = (ToggleButton) view; btn.setEnabled(false); EcoModeControl.changeEcoMode(btn.isChecked()); } public void onToggleComMode(View view) { BackgroundService service = BackgroundService.getInstance(); if (service != null) { // ?????? uiactToggleComModeToggleButton(false); // ???????????? service.terminateOnlineCheck(); // ??????? RouterControl.execRouterCtrl(service.getStateMachine().isComSettingHS() ? CTRL.NAD_COM_NL : CTRL.NAD_COM_HS); } } public void onToggleWifiSpot(View view) { BackgroundService service = BackgroundService.getInstance(); if (service != null) { // ?????? uiactToggleWifiSpotToggleButton(false); // ???????????? service.terminateOnlineCheck(); // Wi-Fi?????????? RouterControl.execRouterCtrl(service.getStateMachine().isWifiSpotEnabled() ? CTRL.NAD_WIFI_SPOT_OFF : CTRL.NAD_WIFI_SPOT_ON); } } public void onWizard(View view) { InitialConfigurationWizardActivity.startWizard(this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Const.REQUEST_ENABLE_BT) { // ???????????????? if (resultCode != RESULT_OK) { Logger.i("ACTION_REQUEST_ENABLE canceled"); BackgroundService service = BackgroundService.getInstance(); if (service == null) { Logger.e("service is null on MainActivity.onActivityResult"); return; } // ???????????????? service.onBluetoothConnected(false); } } super.onActivityResult(requestCode, resultCode, data); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static public enum ACTIVITY_FLAG { NONE, ACTION_DIALOG, BT_ENABLING, INIT_WIZARD } @SuppressLint("InlinedApi") public static void startActivity(Context context, ACTIVITY_FLAG flag) { Intent actIntent = new Intent(context, MainActivity.class); actIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY); // Bluetooth???????? if (flag == ACTIVITY_FLAG.BT_ENABLING) { actIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); actIntent.putExtra(Const.INTENT_EX_BT_ENABLING, true); } // ???????????? else if (flag == ACTIVITY_FLAG.ACTION_DIALOG) { actIntent.putExtra(Const.INTENT_EX_ACTION_SELECT, true); } // ?????????? else if (flag == ACTIVITY_FLAG.INIT_WIZARD) { // ???????????????????????? if (initWizardDisplayed) { return; } actIntent.putExtra(Const.INTENT_EX_INIT_WIZARD, true); } context.startActivity(actIntent); } public void showActionDialog() { // ?????????? if (Const.getPrefWorking(this)) { // ????? final String[] labels = getClickActionStringArray(true); // ??????? dialog = new AlertDialog.Builder(MainActivity.this).setTitle(R.string.select_action) .setItems(labels, new DialogInterface.OnClickListener() { // ????? public void onClick(DialogInterface dialog, int which) { Context context = getApplicationContext(); // ????????????????????? final String[] values = getClickActionStringArray(false); String action = values[which]; // ???????? if (MyStringUtlis.eqauls(action, getString(R.string.menu_widget_click_action__stop_service))) { BackgroundService service = BackgroundService.getInstance(); if (service != null) { updateAsWorkingOrPausing(context, true); service.stopServiceImmediately(); } } // ?????? else { Intent srvIntent = new Intent(context, BackgroundService.class); srvIntent.putExtra(Const.INTENT_EX_DO_ACTION, action); startService(srvIntent); } } }).show(); } // ??????? else { UIAct.toast(getString(R.string.pausing_long)); } } protected String[] getClickActionStringArray(boolean label) { int id = label ? R.array.entries_menu_widget_click_action_on_choose : R.array.entryValues_menu_widget_click_action_on_choose; ArrayList<ListItem> labelList = CustomizeActionsActivity.constructListItemArrayFromCustomizedData(getResources() .getStringArray(id), Const.getPrefWidgetClickActionCustomizedData(this)); int validCount = 0; for (ListItem listItem : labelList) { if (listItem.checked) { validCount++; } } int index = 0; String[] labels = new String[validCount]; for (ListItem listItem : labelList) { if (listItem.checked) { labels[index++] = listItem.label; } } return labels; } protected void updateAsWorking(boolean wifiEnabled, boolean suppCompleted) { // UI?? UIAct.postActivityButton(true, true, wifiEnabled, suppCompleted, null, null, null); UIAct.postActivityInfo(R.drawable.icon_wimax_gray_batt_na, getString(R.string.processing), null, null); // ???????? DefaultWidgetProvider.updateAsWorkingOrPausing(this, true); } public static void updateAsWorkingOrPausing(Context context, boolean working) { // UI?? UIAct.postActivityButton(false, false, null, null, null, null, null); UIAct.postActivityInfo(R.drawable.icon_wimax_gray_batt_na, context.getString(working ? R.string.processing : R.string.pausing_en), null, null); // ???????? DefaultWidgetProvider.updateAsWorkingOrPausing(context, working); } public static void setInitWizardDisplayed(boolean displayed) { initWizardDisplayed = displayed; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void uiactSetMessage(String wdText, String trigger, String state) { if (wdText != null) { ((TextView) findViewById(R.id.wdText)).setText(wdText); } // ??????? // if (trigger != null) { // ((TextView) findViewById(R.id.triggerText)).setText(trigger); // } // if (state != null) { // ((TextView) findViewById(R.id.stateText)).setText(state); // } } public void uiactSwitchImage(Integer wdImageId) { if (wdImageId != null) { ((ImageView) (findViewById(R.id.wdImage))).setImageResource(wdImageId); } } public void uiactSetRouterToggleButton(Boolean enable, Boolean suppCompleted) { Button routerButton = ((Button) (findViewById(R.id.toggleRouter))); if (enable != null) { routerButton.setEnabled(enable); } if (suppCompleted != null) { routerButton.setText(suppCompleted ? R.string.standby : R.string.resume); } } public void uiactSetWifiToggleButton(Boolean enable, Boolean wifiEnabled) { ToggleButton wifiToggle = (ToggleButton) findViewById(R.id.toggleWifi); if (enable != null) { wifiToggle.setEnabled(enable); } if (wifiEnabled != null) { wifiToggle.setChecked(wifiEnabled); } } public void uiactToggleWorkingToggleButton(boolean enable) { ToggleButton workingToggle = (ToggleButton) findViewById(R.id.toggleWorking); workingToggle.setEnabled(enable); } public void uiactToggleComModeToggleButton(boolean enable) { ToggleButton workingToggle = (ToggleButton) findViewById(R.id.toggleComMode); workingToggle.setEnabled(enable); } public void uiactToggleWifiSpotToggleButton(boolean enable) { ToggleButton workingToggle = (ToggleButton) findViewById(R.id.toggleWifiSpot); workingToggle.setEnabled(enable); } public void uiactSetEcoChargeToggleButton(Boolean suppCompleted, Boolean ecoCharge) { //Logger.v("uiactSetEcoChargeToggleButton ecoCharge=" + ecoCharge); ToggleButton ecoChargeButton = ((ToggleButton) (findViewById(R.id.toggleEcoCharge))); if (suppCompleted == null || suppCompleted.booleanValue() == false || ecoCharge == null) { ecoChargeButton.setEnabled(false); ecoChargeButton.setChecked(false); } else { ecoChargeButton.setEnabled(true); ecoChargeButton.setChecked(ecoCharge.booleanValue()); } } public void uiactSetNadToggleButton(Boolean suppCompleted, COM_TYPE comSetting, Boolean wifiSpot) { ToggleButton bm = ((ToggleButton) (findViewById(R.id.toggleComMode))); if (suppCompleted == null || suppCompleted.booleanValue() == false) { bm.setEnabled(false); } else if (comSetting != null && comSetting != COM_TYPE.NA) { bm.setEnabled(true); bm.setChecked(comSetting == COM_TYPE.HIGH_SPEED); } ToggleButton bw = ((ToggleButton) (findViewById(R.id.toggleWifiSpot))); if (suppCompleted == null || suppCompleted.booleanValue() == false) { bw.setEnabled(false); } else if (wifiSpot != null) { bw.setEnabled(true); bw.setChecked(wifiSpot.booleanValue()); } toggleNadLayout(); } public void toggleNadLayout() { LinearLayout layoutWm = (LinearLayout) findViewById(R.id.layoutWm); LinearLayout layoutNad = (LinearLayout) findViewById(R.id.layoutNad); // LinearLayout layoutKeepHs = (LinearLayout) findViewById(R.id.layoutKeepHs); LayoutParams lpWm = layoutWm.getLayoutParams(); LayoutParams lpNad = layoutNad.getLayoutParams(); // LayoutParams lpKeepHs = layoutKeepHs.getLayoutParams(); // WM/NAD?????? if (RouterControlByHttp.isNad()) { lpWm.height = 0; lpNad.height = LayoutParams.WRAP_CONTENT; // lpKeepHs.height = LayoutParams.WRAP_CONTENT; } else { lpWm.height = LayoutParams.WRAP_CONTENT; lpNad.height = 0; // lpKeepHs.height = 0; } // ??????? setLayoutMargin(); } private void setLayoutMargin() { DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); int height = (int) (displayMetrics.heightPixels / displayMetrics.density); //Logger.v("height dip = " + height); int margin = 5; if (height >= 900) { margin = 16; } else if (height >= 400) { margin = 8; } if (margin < 8) { Configuration config = getResources().getConfiguration(); if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { margin = 8; } } int[] ids = {R.id.layoutButtonCommon, R.id.layoutSub, R.id.layoutWm, R.id.layoutNad /* R.id.layoutKeepHs*/}; for (int id : ids) { LinearLayout layout = (LinearLayout) findViewById(id); if (layout != null && layout.getLayoutParams() instanceof MarginLayoutParams) { MarginLayoutParams mlp = (MarginLayoutParams) layout.getLayoutParams(); mlp.topMargin = mlp.height == 0 ? 0 : AndroidUtils.dip2pixel(this, margin); } } } }