From 8a8cb94c16a0bf99c75b04755a33fd60e17e2c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Sch=C3=A4ttgen?= Date: Tue, 5 Jun 2018 15:37:20 +0200 Subject: [PATCH] Started working on a single progressbar when all periods are the same --- .../java/me/impy/aegis/ui/MainActivity.java | 60 +++++++++++++++++++ .../aegis/ui/views/KeyProfileAdapter.java | 26 +++++++- .../impy/aegis/ui/views/KeyProfileView.java | 13 ++++ app/src/main/res/layout/activity_main.xml | 17 ++++++ app/src/main/res/layout/card_keyprofile.xml | 1 + 5 files changed, 116 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/me/impy/aegis/ui/MainActivity.java b/app/src/main/java/me/impy/aegis/ui/MainActivity.java index 4c603dc0..f0789c62 100644 --- a/app/src/main/java/me/impy/aegis/ui/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/ui/MainActivity.java @@ -1,17 +1,22 @@ package me.impy.aegis.ui; import android.Manifest; +import android.animation.ObjectAnimator; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; +import android.graphics.PorterDuff; import android.graphics.Rect; +import android.os.Handler; import android.support.design.widget.BottomSheetDialog; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; +import android.view.animation.LinearInterpolator; import android.widget.LinearLayout; +import android.widget.ProgressBar; import android.widget.Toast; import com.getbase.floatingactionbutton.FloatingActionsMenu; @@ -45,9 +50,12 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen private AegisApplication _app; private DatabaseManager _db; private KeyProfileView _keyProfileView; + private ProgressBar _progressBar; private Menu _menu; private FloatingActionsMenu _fabMenu; + private Handler _uiHandler; + private boolean _running = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -55,6 +63,8 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen _app = (AegisApplication) getApplication(); _db = _app.getDatabaseManager(); + _uiHandler = new Handler(); + // set up the main view setContentView(R.layout.activity_main); @@ -63,6 +73,10 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen _keyProfileView.setListener(this); _keyProfileView.setShowIssuer(getPreferences().isIssuerVisible()); + _progressBar = findViewById(R.id.progressBar); + int primaryColorId = getResources().getColor(R.color.colorPrimary); + _progressBar.getProgressDrawable().setColorFilter(primaryColorId, PorterDuff.Mode.SRC_IN); + // set up the floating action button _fabMenu = findViewById(R.id.fab); findViewById(R.id.fab_enter).setOnClickListener(view -> { @@ -417,6 +431,52 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen for (DatabaseEntry entry : _db.getKeys()) { _keyProfileView.addKey(new KeyProfile(entry)); } + + if(_keyProfileView.allSamePeriod()) + { + startRefreshLoop(); + } + } + + public void refreshCode() + { + _keyProfileView.refresh(); + KeyProfile keyProfile = _keyProfileView.getKeyProfile(0); + + // reset the progress bar + int maxProgress = _progressBar.getMax(); + _progressBar.setProgress(maxProgress); + + // calculate the progress the bar should start at + long millisTillRotation = keyProfile.getEntry().getInfo().getMillisTillNextRotation(); + long period = keyProfile.getEntry().getInfo().getPeriod() * maxProgress; + int currentProgress = maxProgress - (int) ((((double) period - millisTillRotation) / period) * maxProgress); + + // start progress animation + ObjectAnimator animation = ObjectAnimator.ofInt(_progressBar, "progress", currentProgress, 0); + animation.setDuration(millisTillRotation); + animation.setInterpolator(new LinearInterpolator()); + animation.start(); + } + + public void startRefreshLoop() { + if (_running) { + return; + } + _running = true; + + KeyProfile keyProfile = _keyProfileView.getKeyProfile(0); + + refreshCode(); + _uiHandler.postDelayed(new Runnable() { + @Override + public void run() { + if (_running) { + refreshCode(); + _uiHandler.postDelayed(this, keyProfile.getEntry().getInfo().getMillisTillNextRotation()); + } + } + }, keyProfile.getEntry().getInfo().getMillisTillNextRotation()); } private void updateLockIcon() { diff --git a/app/src/main/java/me/impy/aegis/ui/views/KeyProfileAdapter.java b/app/src/main/java/me/impy/aegis/ui/views/KeyProfileAdapter.java index ec316583..0a3ecb20 100644 --- a/app/src/main/java/me/impy/aegis/ui/views/KeyProfileAdapter.java +++ b/app/src/main/java/me/impy/aegis/ui/views/KeyProfileAdapter.java @@ -49,6 +49,10 @@ public class KeyProfileAdapter extends RecyclerView.Adapter im notifyDataSetChanged(); } + public ArrayList getKeys() { + return _keyProfiles; + } + public void replaceKey(KeyProfile newProfile) { KeyProfile oldProfile = getKeyByUUID(newProfile.getEntry().getUUID()); int position = _keyProfiles.indexOf(oldProfile); @@ -108,7 +112,12 @@ public class KeyProfileAdapter extends RecyclerView.Adapter im public void onBindViewHolder(final KeyProfileHolder holder, int position) { final KeyProfile profile = _keyProfiles.get(position); holder.setData(profile, _showIssuer); - holder.startRefreshLoop(); + + if(!allSamePeriod()) + { + holder.startRefreshLoop(); + } + holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -125,6 +134,21 @@ public class KeyProfileAdapter extends RecyclerView.Adapter im }); } + public boolean allSamePeriod() + { + ArrayList profiles = getKeys(); + int period = profiles.get(0).getEntry().getInfo().getPeriod(); + + for (KeyProfile profile : profiles) { + if(period != profile.getEntry().getInfo().getPeriod()) + { + return false; + } + } + + return true; + } + @Override public int getItemCount() { return _keyProfiles.size(); diff --git a/app/src/main/java/me/impy/aegis/ui/views/KeyProfileView.java b/app/src/main/java/me/impy/aegis/ui/views/KeyProfileView.java index d78b7ac1..ce28f703 100644 --- a/app/src/main/java/me/impy/aegis/ui/views/KeyProfileView.java +++ b/app/src/main/java/me/impy/aegis/ui/views/KeyProfileView.java @@ -9,6 +9,9 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import java.lang.reflect.Array; +import java.util.ArrayList; + import me.impy.aegis.R; import me.impy.aegis.db.DatabaseEntry; import me.impy.aegis.helpers.SimpleItemTouchHelperCallback; @@ -68,6 +71,16 @@ public class KeyProfileView extends Fragment implements KeyProfileAdapter.Listen _adapter.notifyDataSetChanged(); } + public boolean allSamePeriod() + { + return _adapter.allSamePeriod(); + } + + public KeyProfile getKeyProfile(int index) + { + return _adapter.getKeys().get(index); + } + public void addKey(KeyProfile profile) { _adapter.addKey(profile); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3be9deed..830a8c17 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -15,6 +15,23 @@ android:layout_width="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> + + + + + +