From bb85201ac12bfd9ae8dba8042ed823d62acda490 Mon Sep 17 00:00:00 2001 From: Kai-Chun Lin Date: Sat, 4 Jun 2016 06:19:11 +0800 Subject: [PATCH] Fix #849 by caching previous states --- .../secupwn/aimsicd/data/DeviceUpdate.java | 161 ++++++++++++++++++ .../aimsicd/ui/fragments/DeviceFragment.java | 94 +++++----- 2 files changed, 214 insertions(+), 41 deletions(-) create mode 100644 AIMSICD/src/main/java/com/secupwn/aimsicd/data/DeviceUpdate.java diff --git a/AIMSICD/src/main/java/com/secupwn/aimsicd/data/DeviceUpdate.java b/AIMSICD/src/main/java/com/secupwn/aimsicd/data/DeviceUpdate.java new file mode 100644 index 000000000..58d0aed60 --- /dev/null +++ b/AIMSICD/src/main/java/com/secupwn/aimsicd/data/DeviceUpdate.java @@ -0,0 +1,161 @@ +package com.secupwn.aimsicd.data; + +import com.secupwn.aimsicd.R; +import com.secupwn.aimsicd.service.AimsicdService; +import com.secupwn.aimsicd.utils.Device; + +/** + * Created by Kai on 2016/6/3. + */ + +public class DeviceUpdate { + private static DeviceUpdate lastUpdate; + + public static DeviceUpdate getLastUpdate() { + return lastUpdate; + } + + public static DeviceUpdate getUpdate(AimsicdService mAimsicdService) { + mAimsicdService.getCellTracker().refreshDevice(); + String notAvailable = mAimsicdService.getApplicationContext().getString(R.string.n_a); + lastUpdate = new DeviceUpdate(); + Device mDevice = mAimsicdService.getCellTracker().getDevice(); + lastUpdate.phoneId = mDevice.getPhoneId(); + lastUpdate.locationAreaCode = String.valueOf(mAimsicdService.getCell().getLocationAreaCode()); + lastUpdate.cellId = String.valueOf(mAimsicdService.getCell().getCellId()); + lastUpdate.systemId = String.valueOf(mAimsicdService.getCell().getSid()); + lastUpdate.lteTimingAdvanceInt = mAimsicdService.getCell().getTimingAdvance(); + lastUpdate.lteTimingAdvance = String.valueOf(lastUpdate.lteTimingAdvanceInt); + lastUpdate.primaryScramblingCodeInt = mAimsicdService.getCell().getPrimaryScramblingCode(); + lastUpdate.primaryScramblingCode = String.valueOf(lastUpdate.primaryScramblingCodeInt); + lastUpdate.simCountry = mDevice.getSimCountry().orElse(notAvailable); + lastUpdate.simOperatorId = mDevice.getSimOperator().orElse(notAvailable); + lastUpdate.simOperatorName = mDevice.getSimOperatorName().orElse(notAvailable); + lastUpdate.simImsi = mDevice.getSimSubs().orElse(notAvailable); + lastUpdate.simSerial = mDevice.getSimSerial().orElse(notAvailable); + lastUpdate.deviceType = mDevice.getPhoneType(); + lastUpdate.deviceImei = mDevice.getIMEI(); + lastUpdate.deviceImeiVersion = mDevice.getIMEIv(); + lastUpdate.networkName = mDevice.getNetworkName(); + lastUpdate.networkCode = mDevice.getMncMcc(); + lastUpdate.networkType = mDevice.getNetworkTypeName(); + lastUpdate.dataActivityType = mDevice.getDataActivityType(); + lastUpdate.dataStatus = mDevice.getDataState(); + lastUpdate.networkRoaming = String.valueOf(mDevice.isRoaming()); + return getLastUpdate(); + } + + private DeviceUpdate() { + } + + private String locationAreaCode; + private String cellId; + private String systemId; + private String lteTimingAdvance; + private int lteTimingAdvanceInt; + private String primaryScramblingCode; + private int primaryScramblingCodeInt; + private String simCountry; + private String simOperatorId; + private String simOperatorName; + private String simImsi; + private String simSerial; + private String deviceType; + private String deviceImei; + private String deviceImeiVersion; + private String networkName; + private String networkCode; + private String networkType; + private String dataActivityType; + private String dataStatus; + private String networkRoaming; + private int phoneId; + + public String getLocationAreaCode() { + return locationAreaCode; + } + + public String getCellId() { + return cellId; + } + + public String getSystemId() { + return systemId; + } + + public String getLteTimingAdvance() { + return lteTimingAdvance; + } + + public int getLteTimingAdvanceInt() { + return lteTimingAdvanceInt; + } + + public String getPrimaryScramblingCode() { + return primaryScramblingCode; + } + + public int getPrimaryScramblingCodeInt() { + return primaryScramblingCodeInt; + } + + public String getSimCountry() { + return simCountry; + } + + public String getSimOperatorId() { + return simOperatorId; + } + + public String getSimOperatorName() { + return simOperatorName; + } + + public String getSimImsi() { + return simImsi; + } + + public String getSimSerial() { + return simSerial; + } + + public String getDeviceType() { + return deviceType; + } + + public String getDeviceImei() { + return deviceImei; + } + + public String getDeviceImeiVersion() { + return deviceImeiVersion; + } + + public String getNetworkName() { + return networkName; + } + + public String getNetworkCode() { + return networkCode; + } + + public String getNetworkType() { + return networkType; + } + + public String getDataActivityType() { + return dataActivityType; + } + + public String getDataStatus() { + return dataStatus; + } + + public String getNetworkRoaming() { + return networkRoaming; + } + + public int getPhoneId() { + return phoneId; + } +} diff --git a/AIMSICD/src/main/java/com/secupwn/aimsicd/ui/fragments/DeviceFragment.java b/AIMSICD/src/main/java/com/secupwn/aimsicd/ui/fragments/DeviceFragment.java index 7a2bdaf60..b12973865 100644 --- a/AIMSICD/src/main/java/com/secupwn/aimsicd/ui/fragments/DeviceFragment.java +++ b/AIMSICD/src/main/java/com/secupwn/aimsicd/ui/fragments/DeviceFragment.java @@ -21,11 +21,11 @@ import com.kaichunlin.transition.animation.AnimationManager; import com.secupwn.aimsicd.R; +import com.secupwn.aimsicd.data.DeviceUpdate; import com.secupwn.aimsicd.service.AimsicdService; import com.secupwn.aimsicd.service.CellTracker; import com.secupwn.aimsicd.ui.widget.HighlightTextView; import com.secupwn.aimsicd.utils.Cell; -import com.secupwn.aimsicd.utils.Device; import com.secupwn.aimsicd.utils.Helpers; import com.squareup.okhttp.Callback; import com.squareup.okhttp.OkHttpClient; @@ -138,12 +138,18 @@ public void onViewCreated(View view, Bundle savedInstanceState) { public void onResume() { super.onResume(); - if (!mBound) { + DeviceUpdate deviceUpdate = DeviceUpdate.getLastUpdate(); + if (deviceUpdate != null) { + updateUI(deviceUpdate, false); + } + if (mBound) { + swipeRefreshLayout.setRefreshing(true); + onRefresh(); + } else { // Bind to LocalService Intent intent = new Intent(mContext, AimsicdService.class); mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } - updateUI(); } @Override @@ -170,7 +176,7 @@ public void onServiceConnected(ComponentName name, IBinder service) { // We've bound to LocalService, cast the IBinder and get LocalService instance mAimsicdService = ((AimsicdService.AimscidBinder) service).getService(); mBound = true; - updateUI(); + updateUI(DeviceUpdate.getUpdate(mAimsicdService), true); } @Override @@ -180,70 +186,75 @@ public void onServiceDisconnected(ComponentName arg0) { } }; - private void updateUI() { - if (mBound) { - final AnimationManager ani = new AnimationManager(); + private void updateHighlightTextView(HighlightTextView view, String value, boolean animate, AnimationManager ani) { + if (animate) { + view.updateText(value, ani); + } else { + view.setText(value); + } + } - mAimsicdService.getCellTracker().refreshDevice(); - Device mDevice = mAimsicdService.getCellTracker().getDevice(); - switch (mDevice.getPhoneId()) { + private void updateUI(DeviceUpdate deviceUpdate, boolean animate) { + if (!animate || mBound) { + final AnimationManager ani = animate ? new AnimationManager() : null; + switch (deviceUpdate.getPhoneId()) { case TelephonyManager.PHONE_TYPE_NONE: // Maybe bad! case TelephonyManager.PHONE_TYPE_SIP: // Maybe bad! case TelephonyManager.PHONE_TYPE_GSM: { - locationAreaCodeView.updateText(String.valueOf(mAimsicdService.getCell().getLocationAreaCode()), ani); + updateHighlightTextView(locationAreaCodeView, deviceUpdate.getLocationAreaCode(), animate, ani); gsmCellIdTableRow.setVisibility(View.VISIBLE); - cellIdView.updateText(String.valueOf(mAimsicdService.getCell().getCellId()), ani); + updateHighlightTextView(cellIdView, deviceUpdate.getCellId(), animate, ani); break; } case TelephonyManager.PHONE_TYPE_CDMA: { cdmaNetworkIdRow.setVisibility(View.VISIBLE); - networkIdView.updateText(String.valueOf(mAimsicdService.getCell().getLocationAreaCode()), ani); + updateHighlightTextView(networkIdView, deviceUpdate.getLocationAreaCode(), animate, ani); cdmaSystemIdRow.setVisibility(View.VISIBLE); - systemIdView.updateText(String.valueOf(mAimsicdService.getCell().getSid()), ani); + updateHighlightTextView(systemIdView, deviceUpdate.getSystemId(), animate, ani); cdmaBaseIdRow.setVisibility(View.VISIBLE); - baseIdView.updateText(String.valueOf(mAimsicdService.getCell().getCellId()), ani); + updateHighlightTextView(baseIdView, deviceUpdate.getCellId(), animate, ani); break; } default: - log.error("unknown phone type: " + mDevice.getPhoneId()); + log.error("unknown phone type: " + deviceUpdate.getPhoneId()); } - if (mAimsicdService.getCell().getTimingAdvance() != Integer.MAX_VALUE) { + if (deviceUpdate.getLteTimingAdvanceInt() != Integer.MAX_VALUE) { lteTimingAdvanceRow.setVisibility(View.VISIBLE); - lteTimingAdvanceView.updateText(String.valueOf(mAimsicdService.getCell().getTimingAdvance()), ani); + updateHighlightTextView(lteTimingAdvanceView, deviceUpdate.getLteTimingAdvance(), animate, ani); } else { lteTimingAdvanceRow.setVisibility(View.GONE); } - if (mAimsicdService.getCell().getPrimaryScramblingCode() != Integer.MAX_VALUE) { - primaryScramblingCodeView.updateText(String.valueOf(mAimsicdService.getCell().getPrimaryScramblingCode()), ani); + if (deviceUpdate.getPrimaryScramblingCodeInt() != Integer.MAX_VALUE) { + updateHighlightTextView(primaryScramblingCodeView, deviceUpdate.getPrimaryScramblingCode(), animate, ani); primaryScramblingCodeRow.setVisibility(View.VISIBLE); } - String notAvailable = getString(R.string.n_a); - - simCountryView.updateText(mDevice.getSimCountry().orElse(notAvailable), ani); - simOperatorIdView.updateText(mDevice.getSimOperator().orElse(notAvailable), ani); - simOperatorNameView.updateText(mDevice.getSimOperatorName().orElse(notAvailable), ani); - simImsiView.updateText(mDevice.getSimSubs().orElse(notAvailable), ani); - simSerialView.updateText(mDevice.getSimSerial().orElse(notAvailable), ani); - - deviceTypeView.updateText(mDevice.getPhoneType(), ani); - deviceImeiView.updateText(mDevice.getIMEI(), ani); - deviceImeiVersionView.updateText(mDevice.getIMEIv(), ani); - networkNameView.updateText(mDevice.getNetworkName(), ani); - networkCodeView.updateText(mDevice.getMncMcc(), ani); - networkTypeView.updateText(mDevice.getNetworkTypeName(), ani); - - dataActivityTypeView.updateText(mDevice.getDataActivityType(), ani); - dataStatusView.updateText(mDevice.getDataState(), ani); - networkRoamingView.updateText(String.valueOf(mDevice.isRoaming()), ani); - - ani.startAnimation(5000); + updateHighlightTextView(simCountryView, deviceUpdate.getSimCountry(), animate, ani); + updateHighlightTextView(simOperatorIdView, deviceUpdate.getSimOperatorId(), animate, ani); + updateHighlightTextView(simOperatorNameView, deviceUpdate.getSimOperatorName(), animate, ani); + updateHighlightTextView(simImsiView, deviceUpdate.getSimImsi(), animate, ani); + updateHighlightTextView(simSerialView, deviceUpdate.getSimSerial(), animate, ani); + + updateHighlightTextView(deviceTypeView, deviceUpdate.getDeviceType(), animate, ani); + updateHighlightTextView(deviceImeiView, deviceUpdate.getDeviceImei(), animate, ani); + updateHighlightTextView(deviceImeiVersionView, deviceUpdate.getDeviceImeiVersion(), animate, ani); + updateHighlightTextView(networkNameView, deviceUpdate.getNetworkName(), animate, ani); + updateHighlightTextView(networkCodeView, deviceUpdate.getNetworkCode(), animate, ani); + updateHighlightTextView(networkTypeView, deviceUpdate.getNetworkType(), animate, ani); + + updateHighlightTextView(dataActivityTypeView, deviceUpdate.getDataActivityType(), animate, ani); + updateHighlightTextView(dataStatusView, deviceUpdate.getDataStatus(), animate, ani); + updateHighlightTextView(networkRoamingView, deviceUpdate.getNetworkRoaming(), animate, ani); + + if (animate) { + ani.startAnimation(5000); + } } } @@ -336,7 +347,7 @@ private void processFinish(Cell cell) { if (cell.isValid()) { mAimsicdService.setCell(cell); Helpers.msgShort(mContext, getActivity().getString(R.string.refreshed_cell_id_info)); // TODO re-translating other languages - updateUI(); + updateUI(DeviceUpdate.getUpdate(mAimsicdService), true); swipeRefreshLayout.setRefreshing(false); } } @@ -345,5 +356,6 @@ private void processFinish(Cell cell) { private void refreshFailed() { Helpers.msgShort(mContext, "Failed to refresh CellId. Check network connection."); swipeRefreshLayout.setRefreshing(false); + updateUI(DeviceUpdate.getUpdate(mAimsicdService), true); } }