Started working on a single progressbar when all periods are the same

pull/41/head
Michael Schättgen 7 years ago committed by Alexander Bakker
parent 964fc72fba
commit 8a8cb94c16

@ -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() {

@ -49,6 +49,10 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
notifyDataSetChanged();
}
public ArrayList<KeyProfile> 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<KeyProfileHolder> 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<KeyProfileHolder> im
});
}
public boolean allSamePeriod()
{
ArrayList<KeyProfile> 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();

@ -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);
}

@ -15,6 +15,23 @@
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<LinearLayout
android:orientation="horizontal"
android:padding="0dp"
android:layout_margin="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:id="@+id/progressBar"
android:max="1000"
android:layout_weight="1"/>
</LinearLayout>
<!-- note: the fab should always be the last element to be sure it's displayed on top -->
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/fab"

@ -109,6 +109,7 @@
android:layout_width="wrap_content"
android:layout_height="4dp"
android:id="@+id/progressBar"
android:visibility="invisible"
android:max="1000"
android:layout_weight="1"/>
</LinearLayout>

Loading…
Cancel
Save