Order is now getting saved properly

Also enabled Java 8
This commit is contained in:
Michael Schättgen 2016-10-25 23:53:33 +02:00
parent 036dd2b2cb
commit 22524136a7
7 changed files with 106 additions and 25 deletions

2
.idea/misc.xml generated
View file

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View file

@ -10,6 +10,9 @@ android {
targetSdkVersion 24
versionCode 1
versionName "1.0"
jackOptions {
enabled true
}
}
buildTypes {
release {
@ -17,6 +20,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}

View file

@ -1,6 +1,7 @@
package me.impy.aegis;
import java.io.Serializable;
import java.security.Key;
import me.impy.aegis.crypto.KeyInfo;
@ -9,5 +10,15 @@ public class KeyProfile implements Serializable {
public String Icon;
public String Code;
public KeyInfo Info;
public int Order;
public int ID;
public int compareTo(KeyProfile another) {
if (this.Order>another.Order){
return -1;
}else{
return 1;
}
}
}

View file

@ -2,6 +2,7 @@ package me.impy.aegis;
import android.animation.ObjectAnimator;
import android.os.Handler;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
@ -16,11 +17,13 @@ import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import me.impy.aegis.crypto.OTP;
import me.impy.aegis.db.Database;
import me.impy.aegis.helpers.ItemTouchHelperAdapter;
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements ItemTouchHelperAdapter {
@ -52,6 +55,24 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
public void onItemMove(int firstPosition, int secondPosition) {
Collections.swap(mKeyProfiles, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition);
mKeyProfiles.get(firstPosition).Order = secondPosition;
adjustOrder(secondPosition);
}
private void adjustOrder(int startPosition)
{
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override
public int compare(KeyProfile keyProfile, KeyProfile t1) {
return keyProfile.Order - t1.Order;
}
};
for(int i = startPosition; i < mKeyProfiles.size(); i++)
{
mKeyProfiles.get(i).Order = i + 1;
}
}
// Create new views (invoked by the layout manager)

View file

@ -23,6 +23,8 @@ import android.widget.Toast;
import com.yarolegovich.lovelydialog.LovelyTextInputDialog;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import me.impy.aegis.crypto.CryptoUtils;
import me.impy.aegis.crypto.OTP;
@ -111,6 +113,13 @@ public class MainActivity extends AppCompatActivity {
touchHelper.attachToRecyclerView(rvKeyProfiles);
rvKeyProfiles.setAdapter(mKeyProfileAdapter);
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override
public int compare(KeyProfile keyProfile, KeyProfile t1) {
return keyProfile.Order - t1.Order;
}
};
Collections.sort(mKeyProfiles, comparator);
try {
for (KeyProfile profile : database.getKeys()) {
@ -149,6 +158,7 @@ public class MainActivity extends AppCompatActivity {
return;
}
keyProfile.Order = mKeyProfiles.size() + 1;
keyProfile.Code = otp;
mKeyProfiles.add(keyProfile);
mKeyProfileAdapter.notifyDataSetChanged();
@ -162,12 +172,41 @@ public class MainActivity extends AppCompatActivity {
}
}
private void adjustOrder(int startPosition)
{
Comparator<KeyProfile> comparator = new Comparator<KeyProfile>() {
@Override
public int compare(KeyProfile keyProfile, KeyProfile t1) {
return keyProfile.Order - t1.Order;
}
};
for(int i = startPosition; i < mKeyProfiles.size(); i++)
{
mKeyProfiles.get(i).Order = i + 1;
}
}
@Override
protected void onResume() {
super.onResume();
setPreferredTheme();
}
@Override
protected void onPause() {
for(int i = 0; i < mKeyProfiles.size(); i++)
{
try {
database.updateKey(mKeyProfiles.get(i));
} catch (Exception e) {
e.printStackTrace();
}
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

View file

@ -6,6 +6,7 @@ import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import me.impy.aegis.KeyProfile;
@ -43,8 +44,8 @@ public class Database {
}
public void updateKey(KeyProfile profile) throws Exception {
db.execSQL("update otp set name=? url=? where id=?",
new Object[]{ profile.Name, profile.Info.getURL(), profile.ID });
db.execSQL("update otp set name=?, url=?, 'order'=? where id=?",
new Object[]{ profile.Name, profile.Info.getURL(), profile.Order, profile.ID });
}
public void removeKey(KeyProfile profile) {
@ -53,19 +54,20 @@ public class Database {
public List<KeyProfile> getKeys() throws Exception {
List<KeyProfile> list = new ArrayList<>();
Cursor cursor = db.rawQuery("select * from otp", null);
Cursor cursor = db.rawQuery("select * from otp order by 'order' desc", null);
try {
while (cursor.moveToNext()) {
KeyProfile profile = new KeyProfile();
profile.ID = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
profile.Name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
profile.Order = cursor.getInt(cursor.getColumnIndexOrThrow("order"));
String url = cursor.getString(cursor.getColumnIndexOrThrow("url"));
profile.Info = KeyInfo.FromURL(url);
list.add(profile);
}
Collections.sort(list, (a, b) -> b.compareTo(a));
return list;
} finally {
cursor.close();

View file

@ -18,7 +18,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
"create table otp (" +
"id integer primary key autoincrement, " +
"name varchar not null, " +
"url varchar not null)";
"url varchar not null, " +
"'order' integer)";
public DatabaseHelper(Context context, String filename) {
super(context, filename, null, Version);