Skip to content

Commit

Permalink
UpdateUI in a different thread to avoid extra lagging.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Aug 18, 2020
1 parent 7db7cc9 commit ece1d70
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
Expand Up @@ -3,24 +3,19 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
Expand Down Expand Up @@ -93,9 +88,65 @@ public void setContext(Context context) {
}

public Context context;
private Thread updateUIThread;
private static ThreatIndicatorGLRenderer.ThreatLevel mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

final Activity activity = getActivity();
if (activity != null) {
activity.setTitle(getResources().getString(R.string.drawer_overview));
}

dbSession = HostageApplication.getInstances().getDaoSession();
daoHelper = new DAOHelper(dbSession, getActivity());


mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
try {
mProfileManager = ProfileManager.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
this.inflater = inflater;
this.container= container;
this.savedInstanceState = savedInstanceState;
mRootView = inflater.inflate(R.layout.fragment_home, container, false);
assignViews();

//addAndroidIcon();
addThreatAnimation();

addConnectionInfoButton();

private ThreatIndicatorGLRenderer.ThreatLevel mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
mDefaultTextColor = mHomeTextName.getCurrentTextColor();

setStateNotActive(true);
setStateNotConnected();

addBroadcastReceiver();

mHomeSwitchConnection = mRootView.findViewById(R.id.home_switch_connection);
mHomeSwitchConnection.setSaveEnabled(false);
setSwitchListener();
mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);

mRootView.findViewById(R.id.home_profile_details).setOnClickListener(v -> {
Fragment fragment = new ProfileManagerFragment();
MainActivity.getInstance().injectFragment(fragment);
});

View.OnClickListener attackClickListener = v -> {
loadAttackListener();
};

mHomeTextAttacks.setOnClickListener(attackClickListener);
mHomeTextSecurity.setOnClickListener(attackClickListener);

return mRootView;
}

private void assignViews() {
mHomeSwitchConnection = mRootView.findViewById(R.id.home_switch_connection);
Expand Down Expand Up @@ -179,6 +230,19 @@ public void setStateConnected() {
isConnected = true;
}

private void startUpdateUiThread(){
updateUIThread = new Thread(){
@Override
public void run() {
MainActivity.getInstance().runOnUiThread(() -> {
updateUI();
});

}
};
updateUIThread.start();
}

public void updateUI() {
loadCurrentProfile();
loadConnectionInfo();
Expand All @@ -192,12 +256,9 @@ public void updateUI() {
updateThreatAnimation(totalAttacks);
}
}

updateTextConnection(totalAttacks);

if (hasActiveListeners) {
setStateActive(true);

// color text according to threat level
changeTextColorThreat(totalAttacks);
//updateAndroidIcon();
Expand All @@ -211,7 +272,6 @@ public void updateUI() {
private void addAndroidIcon(){
mHomeAndroidImage = (ImageView) mRootView.findViewById(R.id.imageview);
mHomeAndroidImage.setImageResource(R.drawable.ic_android_home);

}

private void updateAndroidIcon(){
Expand Down Expand Up @@ -247,7 +307,7 @@ private void changeTextColorThreat(int totalAttacks){
}

}
private void updateThreatAnimation(int totalAttacks) {
private static void updateThreatAnimation(int totalAttacks) {
if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
mThreatLevel = LIVE_THREAT;
} else if (totalAttacks > 0) {
Expand Down Expand Up @@ -294,63 +354,6 @@ private void loadCurrentProfile(){
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

final Activity activity = getActivity();
if (activity != null) {
activity.setTitle(getResources().getString(R.string.drawer_overview));
}

dbSession = HostageApplication.getInstances().getDaoSession();
daoHelper = new DAOHelper(dbSession, getActivity());


mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
try {
mProfileManager = ProfileManager.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
this.inflater = inflater;
this.container= container;
this.savedInstanceState = savedInstanceState;
mRootView = inflater.inflate(R.layout.fragment_home, container, false);
assignViews();

//addAndroidIcon();
addThreatAnimation();

addConnectionInfoButton();

mDefaultTextColor = mHomeTextName.getCurrentTextColor();

setStateNotActive(true);
setStateNotConnected();

addBroadcastReceiver();

mHomeSwitchConnection = mRootView.findViewById(R.id.home_switch_connection);
mHomeSwitchConnection.setSaveEnabled(false);
setSwitchListener();
mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);

mRootView.findViewById(R.id.home_profile_details).setOnClickListener(v -> {
Fragment fragment = new ProfileManagerFragment();
MainActivity.getInstance().injectFragment(fragment);
});

View.OnClickListener attackClickListener = v -> {
loadAttackListener();
};

mHomeTextAttacks.setOnClickListener(attackClickListener);
mHomeTextSecurity.setOnClickListener(attackClickListener);

return mRootView;
}

private void setSwitchListener(){
if (mSwitchChangeListener == null) {
mSwitchChangeListener = (buttonView, isChecked) -> {
Expand Down Expand Up @@ -445,12 +448,12 @@ private void addBroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
if (getUserVisibleHint())
updateUI();
startUpdateUiThread();
}
};
registerBroadcastReceiver();

updateUI();
startUpdateUiThread();
}

private void noServicesAlertDialog(){
Expand All @@ -459,7 +462,6 @@ private void noServicesAlertDialog(){
.setMessage(R.string.profile_no_services_msg)
.setPositiveButton(android.R.string.ok,
(dialog, which) -> {

}).setIcon(android.R.drawable.ic_dialog_info)
.show();

Expand All @@ -481,7 +483,7 @@ private void noConnectionAlertDialog(){
public void onStart() {
super.onStart();
registerBroadcastReceiver();
updateUI();
startUpdateUiThread();
}

@Override
Expand All @@ -494,6 +496,7 @@ public void onDestroy() {
if (mReceiver != null){
unregisterBroadcastReceiver();
}
updateUIThread.stop();
}

private void unbindDrawables(View view) {
Expand Down
Expand Up @@ -109,7 +109,7 @@ public void onDestroy() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreferenceSummary(key);
CheckBoxPreference checkboxPrefMultiStage = (CheckBoxPreference)getPreferenceManager().findPreference("pref_multistage");

if(checkboxPrefMultiStage != null)
checkMultistage(checkboxPrefMultiStage);
}
Expand Down

0 comments on commit ece1d70

Please sign in to comment.