Skip to content

Commit

Permalink
Import/export keys as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
adgad committed Dec 28, 2021
1 parent e49b051 commit f1deb49
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
Expand Down
131 changes: 129 additions & 2 deletions app/src/main/java/com/adgad/kboard/PrefsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,28 @@
import android.view.MenuItem;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
* Created by arjun on 14/03/15.
*/


public class PrefsActivity extends PreferenceActivity {
/**
* Adds intent extras so fragment opens
*/


@Override
protected boolean isValidFragment (String fragmentName) {
return SettingsFragment.class.getName().equals("com.adgad.kboard.PrefsActivity$SettingsFragment");
Expand Down Expand Up @@ -72,16 +86,129 @@ public boolean onCreateOptionsMenu(Menu menu) {
public static class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {

private final Gson gson = new Gson();
SharedPreferences prefs = null;
private final int EXPORT_REQUEST_CODE = 1;
private final int IMPORT_REQUEST_CODE = 2;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.prefs);
initSummary(getPreferenceScreen());

Preference importKeys = (Preference) findPreference("importKeys");

importKeys.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/json");
startActivityForResult(intent, IMPORT_REQUEST_CODE);
return true;
}
});

Preference exportKeys = (Preference) findPreference("exportKeys");

exportKeys.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_TITLE, "kboard-keys.json");
startActivityForResult(intent, EXPORT_REQUEST_CODE);
return true;
}

});




}

public String isToString(InputStream inputStream) {
final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
Reader in = null;
try {
in = new InputStreamReader(inputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
for (; ; ) {
int rsz = 0;
try {
rsz = in.read(buffer, 0, buffer.length);
} catch (IOException e) {
e.printStackTrace();
}
if (rsz < 0)
break;
out.append(buffer, 0, rsz);
}
return out.toString();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
OutputStream outputStream = null;
InputStream inputStream = null;
if (requestCode == EXPORT_REQUEST_CODE) {

String currentVals = prefs.getString(KboardIME.Keys.STORAGE_KEY, "");

// Note: you may use try-with resources if your API is 19+
try {
// InputStream constructor takes File, String (path), or FileDescriptor
// data.getData() holds the URI of the path selected by the picker
Uri uri = data.getData();
outputStream = this.getActivity().getContentResolver().openOutputStream(uri);
outputStream.write(currentVals.getBytes());
outputStream.close();
Toast.makeText(this.getActivity(), "Exported keys to " + uri.getPath(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (requestCode == IMPORT_REQUEST_CODE) {
try {
inputStream = this.getActivity().getContentResolver().openInputStream(data.getData());
String keys = isToString(inputStream);
ArrayList<String> keysAsJson = gson.fromJson(keys, ArrayList.class);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KboardIME.Keys.STORAGE_KEY, keys);
editor.apply();
Toast toast = Toast.makeText(this.getActivity(), "Imported " + keysAsJson.size() + " keys!", Toast.LENGTH_SHORT);
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/xml/prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<Preference android:title="Custom Keys" >
<intent android:action="com.adgad.kboard.CustomKeysActivity"/>
</Preference>
<Preference android:title="Import Keys" android:key="importKeys"/>
<Preference android:title="Export Keys" android:key="exportKeys"/>

<PreferenceCategory
android:title="Options">
Expand Down

0 comments on commit f1deb49

Please sign in to comment.