]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/TokenStore.java
Bump versionCode
[~andy/freeotp] / src / org / fedorahosted / freeotp / TokenStore.java
1 package org.fedorahosted.freeotp;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
7 import org.json.JSONArray;
8 import org.json.JSONException;
9
10 import android.content.Context;
11 import android.content.SharedPreferences;
12
13 public class TokenStore {
14         private static final String NAME = "tokens";
15         private static final String ORDER = "tokenOrder";
16         private SharedPreferences prefs;
17         
18         private List<String> getTokenOrder() {
19                 try {
20                         JSONArray array = new JSONArray(prefs.getString(ORDER, null));
21                         List<String> out = new LinkedList<String>();
22                         for (int i = 0; i < array.length(); i++)
23                                 out.add(array.getString(i));
24                         return out;
25                 } catch (JSONException e) {
26                 } catch (NullPointerException e) {
27                 }
28                 
29                 return new LinkedList<String>();
30         }
31         
32         private SharedPreferences.Editor setTokenOrder(List<String> order) {
33                 JSONArray array = new JSONArray();
34                 for (String key : order)
35                         array.put(key);
36                 
37                 return prefs.edit().putString(ORDER, array.toString());
38         }
39         
40         public TokenStore(Context ctx) {
41                 prefs = ctx.getApplicationContext()
42                                    .getSharedPreferences(NAME, Context.MODE_PRIVATE);
43         }
44         
45         public void add(Token token) {
46                 String key = token.getID();
47                 
48                 if (prefs.contains(key))
49                         return;
50                 
51                 List<String> order = getTokenOrder();
52                 order.add(0, key);
53                 setTokenOrder(order).putString(key, token.toString()).apply();
54         }
55         
56         public void del(int index) {            
57                 List<String> order = getTokenOrder();
58                 String key = order.remove(index);
59                 setTokenOrder(order).remove(key).apply();
60         }
61         
62         public void save(Token token) { 
63                 prefs.edit().putString(token.getID(), token.toString()).apply();
64         }
65         
66         public Token get(int index) {
67                 try {
68                         return new Token(prefs.getString(getTokenOrder().get(index), null));
69                 } catch (TokenUriInvalidException e) {
70                         e.printStackTrace();
71                 } catch (NullPointerException e) {
72                         e.printStackTrace();
73                 }
74                 
75                 return null;
76         }
77         
78         public int getTokenCount() {
79                 return getTokenOrder().size();
80         }
81         
82         public void move(int fromIndex, int toIndex) {
83                 List<String> order = getTokenOrder();
84                 if (fromIndex < 0 || fromIndex > order.size())
85                         return;
86                 if (toIndex < 0 || toIndex > order.size())
87                         return;
88                 
89                 order.add(toIndex, order.remove(fromIndex));
90                 setTokenOrder(order).apply();
91         }
92 }