Java tutorial
/** * Project Name:XPGSdkV4AppBase * File Name:BaseActivity.java * Package Name:com.gizwits.framework.activity * Date:2015-1-27 11:32:52 * Copyright (c) 2014~2015 Xtreme Programming Group, Inc. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.gizwits.framework.activity; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.Toast; import com.gizwits.framework.sdk.CmdCenter; import com.gizwits.framework.sdk.SettingManager; import com.gizwits.framework.utils.Historys; import com.gizwits.powersocket.R; import com.xtremeprog.xpgconnect.XPGWifiDevice; import com.xtremeprog.xpgconnect.XPGWifiDeviceListener; import com.xtremeprog.xpgconnect.XPGWifiSDKListener; import com.xtremeprog.xpgconnect.XPGWifiSSID; // TODO: Auto-generated Javadoc /** * activityXPGWifiDeviceListenerXPGWifiSDKListener??? * . * * @author Lien Li */ public class BaseActivity extends Activity { private boolean isExit = false; private static final String TAG = "BaseActivity"; /** * . */ protected static List<XPGWifiDevice> deviceslist = new ArrayList<XPGWifiDevice>(); /** */ protected static List<XPGWifiDevice> bindlist = new ArrayList<XPGWifiDevice>(); /** The device data map. */ protected static ConcurrentHashMap<String, Object> deviceDataMap; /** The statu map. */ protected static ConcurrentHashMap<String, Object> statuMap; /** * ?. */ protected CmdCenter mCenter; /** * SharePreference?. */ public static SettingManager setmanager; /** ?? */ protected static XPGWifiDevice mXpgWifiDevice; /** The handler. */ @SuppressLint("HandlerLeak") private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { isExit = false; }; }; /** * XPGWifiDeviceListener * <p/> * ? ?????????. */ protected XPGWifiDeviceListener deviceListener = new XPGWifiDeviceListener() { @Override public void didDeviceOnline(XPGWifiDevice device, boolean isOnline) { BaseActivity.this.didDeviceOnline(device, isOnline); } @Override public void didDisconnected(XPGWifiDevice device) { BaseActivity.this.didDisconnected(device); } @Override public void didLogin(XPGWifiDevice device, int result) { BaseActivity.this.didLogin(device, result); } @Override public void didReceiveData(XPGWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int result) { BaseActivity.this.didReceiveData(device, dataMap, result); } }; /** * XPGWifiSDKListener * <p/> * sdk? ??????. */ private XPGWifiSDKListener sdkListener = new XPGWifiSDKListener() { @Override public void didBindDevice(int error, String errorMessage, String did) { BaseActivity.this.didBindDevice(error, errorMessage, did); } @Override public void didChangeUserEmail(int error, String errorMessage) { BaseActivity.this.didChangeUserEmail(error, errorMessage); } @Override public void didChangeUserPassword(int error, String errorMessage) { BaseActivity.this.didChangeUserPassword(error, errorMessage); } @Override public void didChangeUserPhone(int error, String errorMessage) { BaseActivity.this.didChangeUserPhone(error, errorMessage); } @Override public void didDiscovered(int error, List<XPGWifiDevice> devicesList) { BaseActivity.this.didDiscovered(error, devicesList); } @Override public void didGetSSIDList(int error, List<XPGWifiSSID> ssidInfoList) { BaseActivity.this.didGetSSIDList(error, ssidInfoList); } @Override public void didRegisterUser(int error, String errorMessage, String uid, String token) { BaseActivity.this.didRegisterUser(error, errorMessage, uid, token); } /* * @Override public void didRequestSendVerifyCode(int error, String * errorMessage) { BaseActivity.this.didRequestSendVerifyCode(error, * errorMessage); } */ @Override public void didSetDeviceWifi(int error, XPGWifiDevice device) { BaseActivity.this.didSetDeviceWifi(error, device); } @Override public void didUnbindDevice(int error, String errorMessage, String did) { BaseActivity.this.didUnbindDevice(error, errorMessage, did); } @Override public void didUserLogin(int error, String errorMessage, String uid, String token) { BaseActivity.this.didUserLogin(error, errorMessage, uid, token); } @Override public void didUserLogout(int error, String errorMessage) { BaseActivity.this.didUserLogout(error, errorMessage); } public void didGetCaptchaCode(int result, java.lang.String errorMessage, java.lang.String token, java.lang.String captchaId, java.lang.String captchaURL) { BaseActivity.this.didGetCaptchaCode(result, errorMessage, token, captchaId, captchaURL); } public void didRequestSendPhoneSMSCode(int result, java.lang.String errorMessage) { BaseActivity.this.didRequestSendPhoneSMSCode(result, errorMessage); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setmanager = new SettingManager(getApplicationContext()); mCenter = CmdCenter.getInstance(getApplicationContext()); // ?activity?sdk???sdk? mCenter.getXPGWifiSDK().setListener(sdkListener); // activity?app????? Historys.put(this); } /** * ?. * * @param error * ? * @param errorMessage * ? */ protected void didUserLogout(int error, String errorMessage) { } /** * ?. * * @param error * ? * @param errorMessage * ? * @param uid * id * @param token * ? */ protected void didUserLogin(int error, String errorMessage, String uid, String token) { } /** * ?. * * @param error * ? * @param errorMessage * ? * @param did * id */ protected void didUnbindDevice(int error, String errorMessage, String did) { } /** * ?. * * @param error * ? * @param device * */ protected void didSetDeviceWifi(int error, XPGWifiDevice device) { } /** * ???. * * @param error * ? * @param errorMessage * ? */ /* * protected void didRequestSendVerifyCode(int error, String errorMessage) { * * } */ /** * ?? * * @param result * @param errorMessage * @param token * @param captchaId * @param captchaURL */ protected void didGetCaptchaCode(int result, java.lang.String errorMessage, java.lang.String token, java.lang.String captchaId, java.lang.String captcthishaURL) { // Log.e("AppTest", "??" + result + ", " + errorMessage + ", " // + token + ", " + captchaId + ", " + captcthishaURL); } /** * ???? * * @param result * @param errorMessage */ protected void didRequestSendPhoneSMSCode(int result, java.lang.String errorMessage) { } /** * ?. * * @param error * ? * @param errorMessage * ? * @param uid * the id * @param token * the ? */ protected void didRegisterUser(int error, String errorMessage, String uid, String token) { // TODO Auto-generated method stub } /** * ?ssid?. * * @param error * ? * @param ssidInfoList * ssid */ protected void didGetSSIDList(int error, List<XPGWifiSSID> ssidInfoList) { // TODO Auto-generated method stub } /** * ??. * * @param error * ? * @param devicesList * */ protected void didDiscovered(int error, List<XPGWifiDevice> devicesList) { // TODO Auto-generated method stub } /** * ????. * * @param error * ? * @param errorMessage * ? */ protected void didChangeUserPhone(int error, String errorMessage) { // TODO Auto-generated method stub } /** * ???. * * @param error * ? * @param errorMessage * ? */ protected void didChangeUserPassword(int error, String errorMessage) { // TODO Auto-generated method stub } /** * ?. * * @param error * ? * @param errorMessage * ? */ protected void didChangeUserEmail(int error, String errorMessage) { } /** * . * * @param error * ? * @param errorMessage * ? * @param did * id */ protected void didBindDevice(int error, String errorMessage, String did) { } /** * * <p/> * sdk???. * * @param device * * @param dataMap * json? * @param result * ?? */ protected void didReceiveData(XPGWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int result) { } /** * ?. * * @param device * * @param result * ?? */ protected void didLogin(XPGWifiDevice device, int result) { } /** * ?. * * @param device * */ protected void didDisconnected(XPGWifiDevice device) { } /** * . * * @param device * * @param isOnline * ? */ protected void didDeviceOnline(XPGWifiDevice device, boolean isOnline) { } /** * didmacdevice. * * @param mac * the mac * @param did * the did * @return the XPG wifi device */ public static XPGWifiDevice findDeviceByMac(String mac, String did) { XPGWifiDevice xpgdevice = null; Log.i("count", BaseActivity.deviceslist.size() + ""); for (int i = 0; i < BaseActivity.deviceslist.size(); i++) { XPGWifiDevice device = deviceslist.get(i); if (device != null) { Log.i("deivcemac", device.getMacAddress()); if (device != null && device.getMacAddress().equals(mac) && device.getDid().equals(did)) { xpgdevice = device; break; } } } return xpgdevice; } public void onResume() { super.onResume(); // ?activity?sdk???sdk? mCenter.getXPGWifiSDK().setListener(sdkListener); } /** * ? * * @return */ protected void initBindList() { if (bindlist != null && bindlist.size() > 0) bindlist.clear(); for (XPGWifiDevice xpgDevice : deviceslist) { if (xpgDevice.isBind(setmanager.getUid())) { bindlist.add(xpgDevice); } } } /** * ??app */ public void exit() { if (!isExit) { isExit = true; Toast.makeText(getApplicationContext(), getString(R.string.tip_exit), Toast.LENGTH_SHORT).show(); handler.sendEmptyMessageDelayed(0, 2000); } else { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(intent); Historys.exit(); } } /** * ?? * * @param map * the map * @param json * the json * @throws JSONException * the JSON exception */ protected void inputDataToMaps(ConcurrentHashMap<String, Object> map, String json) throws JSONException { Log.i("revjson", json); JSONObject receive = new JSONObject(json); Iterator actions = receive.keys(); while (actions.hasNext()) { String action = actions.next().toString(); Log.i("revjson", "action=" + action); // if (action.equals("cmd") || action.equals("qos") || action.equals("seq") || action.equals("version")) { continue; } JSONObject params = receive.getJSONObject(action); Log.i("revjson", "params=" + params); Iterator it_params = params.keys(); while (it_params.hasNext()) { String param = it_params.next().toString(); Object value = params.get(param); map.put(param, value); Log.i(TAG, "Key:" + param + ";value" + value); } } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { // ?ViewEditText View v = getCurrentFocus(); if (isShouldHideInput(v, ev)) { hideSoftInput(v.getWindowToken()); } } return super.dispatchTouchEvent(ev); } /** * ?EditText????????EditText??? * * @param v * @param event * @return */ private boolean isShouldHideInput(View v, MotionEvent event) { if (v != null && (v instanceof EditText)) { int[] l = { 0, 0 }; v.getLocationInWindow(l); int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth(); if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { // EditText return false; } else { return true; } } // ?EditText??EditView? return false; } /** * ???? * * @param token */ private void hideSoftInput(IBinder token) { if (token != null) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } } }