-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
225 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
app/src/main/java/de/dotwee/micropinner/tools/JsonHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package de.dotwee.micropinner.tools; | ||
|
||
import android.app.NotificationManager; | ||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.util.Log; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import de.dotwee.micropinner.ui.MainActivity; | ||
|
||
/** | ||
* Created by Lukas on 26.06.2015. | ||
*/ | ||
public class JsonHandler { | ||
public final static String ARRAY_KEY = "pins"; | ||
private final static String LOG_TAG = "JsonHandler"; | ||
private SharedPreferences sharedPreferences; | ||
private Context context; | ||
|
||
public JsonHandler(Context context) { | ||
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | ||
this.context = context; | ||
} | ||
|
||
private static JSONArray remove(final int idx, final JSONArray from) { | ||
final List<JSONObject> objs = asList(from); | ||
objs.remove(idx); | ||
|
||
final JSONArray ja = new JSONArray(); | ||
for (final JSONObject obj : objs) { | ||
ja.put(obj); | ||
} | ||
|
||
return ja; | ||
} | ||
|
||
private static List<JSONObject> asList(final JSONArray ja) { | ||
final int len = ja.length(); | ||
final ArrayList<JSONObject> result = new ArrayList<JSONObject>(len); | ||
for (int i = 0; i < len; i++) { | ||
final JSONObject obj = ja.optJSONObject(i); | ||
if (obj != null) { | ||
result.add(obj); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public JSONObject generate(String title, String content, int visibility, int priority, boolean persistent, int notification_id) { | ||
JSONObject jsonObject = new JSONObject(); | ||
try { | ||
jsonObject.put(MainActivity.EXTRA_TITLE, title); | ||
jsonObject.put(MainActivity.EXTRA_CONTENT, content); | ||
jsonObject.put(MainActivity.EXTRA_VISIBILITY, visibility); | ||
jsonObject.put(MainActivity.EXTRA_PRIORITY, priority); | ||
|
||
jsonObject.put(MainActivity.EXTRA_PERSISTENT, persistent); | ||
jsonObject.put(MainActivity.EXTRA_NOTIFICATION, notification_id); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return jsonObject; | ||
} | ||
|
||
public void restore() { | ||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
JSONArray jsonArray = get(); | ||
|
||
if (jsonArray != null) | ||
try { | ||
if (jsonArray.length() != 0) for (int i = 0; i < jsonArray.length(); i++) { | ||
JSONObject jsonObject = jsonArray.getJSONObject(i); | ||
|
||
String title = jsonObject.getString(MainActivity.EXTRA_TITLE), content = jsonObject.getString(MainActivity.EXTRA_CONTENT); | ||
int visibility = jsonObject.getInt(MainActivity.EXTRA_VISIBILITY), | ||
priority = jsonObject.getInt(MainActivity.EXTRA_PRIORITY), | ||
notification_id = jsonObject.getInt(MainActivity.EXTRA_NOTIFICATION); | ||
|
||
boolean persistent = jsonObject.getBoolean(MainActivity.EXTRA_PERSISTENT); | ||
|
||
notificationManager.notify(notification_id, MainActivity.generatePin(context, visibility, priority, notification_id, title, content, persistent)); | ||
Log.i(LOG_TAG, "restore / Restored " + jsonObject.toString()); | ||
} | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private JSONArray get() { | ||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | ||
String s = sharedPreferences.getString(ARRAY_KEY, null); | ||
if (s == null) s = "[]"; | ||
|
||
Log.i(LOG_TAG, "get / Return: " + s); | ||
|
||
try { | ||
return new JSONArray(s); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
Log.i(LOG_TAG, "get / Error while creating JSONArray. Returning null."); | ||
return null; | ||
} | ||
} | ||
|
||
private void write(JSONArray jsonArray) { | ||
if (jsonArray == null || jsonArray.toString().isEmpty()) { | ||
sharedPreferences.edit().putString(ARRAY_KEY, "{}").commit(); | ||
Log.i(LOG_TAG, "write / Writing empty array."); | ||
} | ||
else { | ||
sharedPreferences.edit().putString(ARRAY_KEY, jsonArray.toString()).commit(); | ||
Log.i(LOG_TAG, "write / New array: " + jsonArray.toString()); | ||
} | ||
} | ||
|
||
public void append(String title, String content, int visibility, int priority, boolean persistent, int notification_id) { | ||
JSONObject jsonObject = generate(title, content, visibility, priority, persistent, notification_id); | ||
append(jsonObject); | ||
} | ||
|
||
public void append(JSONObject jsonObject) { | ||
JSONArray jsonArray = get(); | ||
if (jsonArray != null) jsonArray.put(jsonObject); | ||
write(jsonArray); | ||
} | ||
|
||
public void edit(String title, String content, int visibility, int priority, boolean persistent, int notification_id) { | ||
JSONObject jsonObject = generate(title, content, visibility, priority, persistent, notification_id); | ||
remove(notification_id); | ||
append(jsonObject); | ||
} | ||
|
||
public void remove(int notification_id) { | ||
Log.i(LOG_TAG, "remove / Remove " + notification_id); | ||
JSONArray jsonArray = get(), newArray = new JSONArray(); | ||
|
||
try { | ||
if (jsonArray != null) { | ||
for (int i = 0; i < jsonArray.length(); i++) { | ||
Log.i(LOG_TAG, "remove / Checking " + i); | ||
|
||
JSONObject jsonObject = jsonArray.getJSONObject(i); | ||
if (jsonObject.getInt(MainActivity.EXTRA_NOTIFICATION) == notification_id) | ||
Log.i(LOG_TAG, "remove / Skipping " + notification_id); | ||
else newArray.put(jsonObject); | ||
} | ||
} else Log.i(LOG_TAG, "remove / Empty json array."); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
write(newArray); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/java/de/dotwee/micropinner/tools/OnDeleteReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.dotwee.micropinner.tools; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
|
||
import de.dotwee.micropinner.ui.MainActivity; | ||
|
||
/** | ||
* Created by Lukas on 26.06.2015. | ||
*/ | ||
public class OnDeleteReceiver extends BroadcastReceiver { | ||
private final static String LOG_TAG = "OnDeleteReceiver"; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
int i = intent.getIntExtra(MainActivity.EXTRA_NOTIFICATION, -1); | ||
new JsonHandler(context).remove(i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters