|
15 | 15 | import android.view.MenuItem; |
16 | 16 | import android.widget.Toast; |
17 | 17 |
|
| 18 | +import com.google.gson.Gson; |
| 19 | + |
| 20 | +import java.io.FileNotFoundException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.InputStreamReader; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.io.Reader; |
| 26 | +import java.io.UnsupportedEncodingException; |
| 27 | +import java.util.ArrayList; |
| 28 | + |
18 | 29 | /** |
19 | 30 | * Created by arjun on 14/03/15. |
20 | 31 | */ |
| 32 | + |
| 33 | + |
21 | 34 | public class PrefsActivity extends PreferenceActivity { |
22 | 35 | /** |
23 | 36 | * Adds intent extras so fragment opens |
24 | 37 | */ |
25 | 38 |
|
| 39 | + |
26 | 40 | @Override |
27 | 41 | protected boolean isValidFragment (String fragmentName) { |
28 | 42 | return SettingsFragment.class.getName().equals("com.adgad.kboard.PrefsActivity$SettingsFragment"); |
@@ -72,16 +86,129 @@ public boolean onCreateOptionsMenu(Menu menu) { |
72 | 86 | public static class SettingsFragment extends PreferenceFragment |
73 | 87 | implements SharedPreferences.OnSharedPreferenceChangeListener { |
74 | 88 |
|
| 89 | + private final Gson gson = new Gson(); |
| 90 | + SharedPreferences prefs = null; |
| 91 | + private final int EXPORT_REQUEST_CODE = 1; |
| 92 | + private final int IMPORT_REQUEST_CODE = 2; |
| 93 | + |
| 94 | + |
75 | 95 |
|
76 | 96 | @Override |
77 | 97 | public void onCreate(Bundle savedInstanceState) { |
78 | 98 | super.onCreate(savedInstanceState); |
79 | | - |
80 | | - |
| 99 | + prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); |
81 | 100 | // Load the preferences from an XML resource |
82 | 101 | addPreferencesFromResource(R.xml.prefs); |
83 | 102 | initSummary(getPreferenceScreen()); |
84 | 103 |
|
| 104 | + Preference importKeys = (Preference) findPreference("importKeys"); |
| 105 | + |
| 106 | + importKeys.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { |
| 107 | + @Override |
| 108 | + public boolean onPreferenceClick(Preference preference) { |
| 109 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 110 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 111 | + intent.setType("application/json"); |
| 112 | + startActivityForResult(intent, IMPORT_REQUEST_CODE); |
| 113 | + return true; |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + Preference exportKeys = (Preference) findPreference("exportKeys"); |
| 118 | + |
| 119 | + exportKeys.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { |
| 120 | + @Override |
| 121 | + public boolean onPreferenceClick(Preference preference) { |
| 122 | + Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
| 123 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 124 | + intent.setType("application/json"); |
| 125 | + intent.putExtra(Intent.EXTRA_TITLE, "kboard-keys.json"); |
| 126 | + startActivityForResult(intent, EXPORT_REQUEST_CODE); |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + }); |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + public String isToString(InputStream inputStream) { |
| 138 | + final int bufferSize = 1024; |
| 139 | + final char[] buffer = new char[bufferSize]; |
| 140 | + final StringBuilder out = new StringBuilder(); |
| 141 | + Reader in = null; |
| 142 | + try { |
| 143 | + in = new InputStreamReader(inputStream, "UTF-8"); |
| 144 | + } catch (UnsupportedEncodingException e) { |
| 145 | + e.printStackTrace(); |
| 146 | + } |
| 147 | + for (; ; ) { |
| 148 | + int rsz = 0; |
| 149 | + try { |
| 150 | + rsz = in.read(buffer, 0, buffer.length); |
| 151 | + } catch (IOException e) { |
| 152 | + e.printStackTrace(); |
| 153 | + } |
| 154 | + if (rsz < 0) |
| 155 | + break; |
| 156 | + out.append(buffer, 0, rsz); |
| 157 | + } |
| 158 | + return out.toString(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 163 | + super.onActivityResult(requestCode, resultCode, data); |
| 164 | + OutputStream outputStream = null; |
| 165 | + InputStream inputStream = null; |
| 166 | + if (requestCode == EXPORT_REQUEST_CODE) { |
| 167 | + |
| 168 | + String currentVals = prefs.getString(KboardIME.Keys.STORAGE_KEY, ""); |
| 169 | + |
| 170 | + // Note: you may use try-with resources if your API is 19+ |
| 171 | + try { |
| 172 | + // InputStream constructor takes File, String (path), or FileDescriptor |
| 173 | + // data.getData() holds the URI of the path selected by the picker |
| 174 | + Uri uri = data.getData(); |
| 175 | + outputStream = this.getActivity().getContentResolver().openOutputStream(uri); |
| 176 | + outputStream.write(currentVals.getBytes()); |
| 177 | + outputStream.close(); |
| 178 | + Toast.makeText(this.getActivity(), "Exported keys to " + uri.getPath(), Toast.LENGTH_LONG).show(); |
| 179 | + } catch (FileNotFoundException e) { |
| 180 | + e.printStackTrace(); |
| 181 | + } catch (IOException e) { |
| 182 | + e.printStackTrace(); |
| 183 | + } finally { |
| 184 | + try { |
| 185 | + outputStream.close(); |
| 186 | + } catch (IOException e) { |
| 187 | + e.printStackTrace(); |
| 188 | + } |
| 189 | + } |
| 190 | + } else if (requestCode == IMPORT_REQUEST_CODE) { |
| 191 | + try { |
| 192 | + inputStream = this.getActivity().getContentResolver().openInputStream(data.getData()); |
| 193 | + String keys = isToString(inputStream); |
| 194 | + ArrayList<String> keysAsJson = gson.fromJson(keys, ArrayList.class); |
| 195 | + SharedPreferences.Editor editor = prefs.edit(); |
| 196 | + editor.putString(KboardIME.Keys.STORAGE_KEY, keys); |
| 197 | + editor.apply(); |
| 198 | + Toast toast = Toast.makeText(this.getActivity(), "Imported " + keysAsJson.size() + " keys!", Toast.LENGTH_SHORT); |
| 199 | + toast.show(); |
| 200 | + } catch (FileNotFoundException e) { |
| 201 | + e.printStackTrace(); |
| 202 | + } catch (IOException e) { |
| 203 | + e.printStackTrace(); |
| 204 | + } finally { |
| 205 | + try { |
| 206 | + inputStream.close(); |
| 207 | + } catch (IOException e) { |
| 208 | + e.printStackTrace(); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
85 | 212 | } |
86 | 213 |
|
87 | 214 | @Override |
|
0 commit comments