]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/adapters/TokenPersistenceBaseAdapter.java
Tablet UI rewrite
[~andy/freeotp] / src / org / fedorahosted / freeotp / adapters / TokenPersistenceBaseAdapter.java
1 /*
2  * FreeOTP
3  *
4  * Authors: Nathaniel McCallum <npmccallum@redhat.com>
5  *
6  * Copyright (C) 2013  Nathaniel McCallum, Red Hat
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 package org.fedorahosted.freeotp.adapters;
22
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import org.fedorahosted.freeotp.Token;
27 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
28 import org.json.JSONArray;
29 import org.json.JSONException;
30
31 import android.content.Context;
32 import android.content.SharedPreferences;
33
34 public abstract class TokenPersistenceBaseAdapter extends DeleteActionBarBaseAdapter {
35         private static final String NAME = "tokens";
36         private static final String ORDER = "tokenOrder";
37         private final SharedPreferences prefs;
38
39         private List<String> getTokenOrder() {
40                 try {
41                         JSONArray array = new JSONArray(prefs.getString(ORDER, null));
42                         List<String> out = new LinkedList<String>();
43                         for (int i = 0; i < array.length(); i++)
44                                 out.add(array.getString(i));
45                         return out;
46                 } catch (JSONException e) {
47                 } catch (NullPointerException e) {
48                 }
49
50                 return new LinkedList<String>();
51         }
52
53         private SharedPreferences.Editor setTokenOrder(List<String> order) {
54                 JSONArray array = new JSONArray();
55                 for (String key : order)
56                         array.put(key);
57
58                 return prefs.edit().putString(ORDER, array.toString());
59         }
60
61         public TokenPersistenceBaseAdapter(Context ctx) {
62                 prefs = ctx.getApplicationContext()
63                                    .getSharedPreferences(NAME, Context.MODE_PRIVATE);
64         }
65
66         @Override
67         public int getCount() {
68                 return getTokenOrder().size();
69         }
70
71         @Override
72         public Token getItem(int position) {
73                 try {
74                         return new Token(prefs.getString(getTokenOrder().get(position), null));
75                 } catch (TokenUriInvalidException e) {
76                         e.printStackTrace();
77                 } catch (NullPointerException e) {
78                         e.printStackTrace();
79                 }
80
81                 return null;
82         }
83
84         @Override
85         public long getItemId(int position) {
86                 return position;
87         }
88
89         @Override
90         public void move(int fromPosition, int toPosition) {
91                 if (fromPosition == toPosition)
92                         return;
93
94                 List<String> order = getTokenOrder();
95                 if (fromPosition < 0 || fromPosition > order.size())
96                         return;
97                 if (toPosition < 0 || toPosition > order.size())
98                         return;
99
100                 order.add(toPosition, order.remove(fromPosition));
101                 setTokenOrder(order).apply();
102                 notifyDataSetChanged();
103         }
104
105         public void add(String uri) throws TokenUriInvalidException {
106                 Token token = new Token(uri);
107                 String key = token.getID();
108
109                 if (prefs.contains(key))
110                         return;
111
112                 List<String> order = getTokenOrder();
113                 order.add(0, key);
114                 setTokenOrder(order).putString(key, token.toString()).apply();
115                 notifyDataSetChanged();
116         }
117
118         @Override
119         public void delete(int position) {
120                 List<String> order = getTokenOrder();
121                 String key = order.remove(position);
122                 setTokenOrder(order).remove(key).apply();
123                 notifyDataSetChanged();
124         }
125
126         protected void save(Token token) {
127                 prefs.edit().putString(token.getID(), token.toString()).apply();
128         }
129 }