4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 package org.fedorahosted.freeotp.adapters;
23 import java.util.LinkedList;
24 import java.util.List;
26 import org.fedorahosted.freeotp.Token;
27 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
28 import org.json.JSONArray;
29 import org.json.JSONException;
31 import android.content.Context;
32 import android.content.SharedPreferences;
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;
39 private List<String> getTokenOrder() {
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));
46 } catch (JSONException e) {
47 } catch (NullPointerException e) {
50 return new LinkedList<String>();
53 private SharedPreferences.Editor setTokenOrder(List<String> order) {
54 JSONArray array = new JSONArray();
55 for (String key : order)
58 return prefs.edit().putString(ORDER, array.toString());
61 public TokenPersistenceBaseAdapter(Context ctx) {
62 prefs = ctx.getApplicationContext()
63 .getSharedPreferences(NAME, Context.MODE_PRIVATE);
67 public int getCount() {
68 return getTokenOrder().size();
72 public Token getItem(int position) {
74 return new Token(prefs.getString(getTokenOrder().get(position), null));
75 } catch (TokenUriInvalidException e) {
77 } catch (NullPointerException e) {
85 public long getItemId(int position) {
90 public void move(int fromPosition, int toPosition) {
91 if (fromPosition == toPosition)
94 List<String> order = getTokenOrder();
95 if (fromPosition < 0 || fromPosition > order.size())
97 if (toPosition < 0 || toPosition > order.size())
100 order.add(toPosition, order.remove(fromPosition));
101 setTokenOrder(order).apply();
102 notifyDataSetChanged();
105 public void add(String uri) throws TokenUriInvalidException {
106 Token token = new Token(uri);
107 String key = token.getID();
109 if (prefs.contains(key))
112 List<String> order = getTokenOrder();
114 setTokenOrder(order).putString(key, token.toString()).apply();
115 notifyDataSetChanged();
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();
126 protected void save(Token token) {
127 prefs.edit().putString(token.getID(), token.toString()).apply();