Skip to content

Commit

Permalink
Merge branch 'dev-jsoncache'
Browse files Browse the repository at this point in the history
  • Loading branch information
dotWee committed Jul 2, 2015
2 parents 29d2e1c + a8d56ff commit 7bee6eb
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 13 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ MicroPinner is a lightweight dialog-only application, which lets you pin text to
<br> It just want to know when your devices finished its boot-procress, to restore your pins.
+ **Open source**
<br> The whole source-code is available here on Github.
+ **Restore functions**
<br> Pins are saved until you swipe them away.
<br> To restore your pins for example after a reboot, simply open the dialog.
+ **Permanent pins**
<br> Declare your pins as permenent and delete them by clicking on them.
+ **Choosable priority**
Expand Down Expand Up @@ -71,6 +74,11 @@ MicroPinner is a lightweight dialog-only application, which lets you pin text to
- sha1 checksum: *0b95003458962e9124f460ded4b33cbaf2f930fd*
- direct download: [Github](https://github.com/dotWee/MicroPinner/releases/download/release-v1.4/release_v1.4.apk)

6. Version: v1.5 (Implemented restore-functions)
- date: 02nd July 2015
- sha1 checksum: *52a5266009413e13e1228a7166e1b6523bd094ca*
- direct download: [Github](https://github.com/dotWee/MicroPinner/releases/download/release-v1.4/release_v1.4.apk)

## Questions / Issues / Bugs

Please check the [FAQ](/docs/FAQ.md) first.
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "de.dotwee.micropinner"
minSdkVersion 16
targetSdkVersion 22
versionCode 14
versionName "v1.4"
versionCode 15
versionName "v1.5"
}
buildTypes {
debug {
Expand Down
Binary file added app/release_v1.5.apk
Binary file not shown.
10 changes: 7 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.dotwee.micropinner">
<manifest package="de.dotwee.micropinner"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Expand All @@ -27,7 +27,11 @@
android:label="@string/edit_name"
android:theme="@style/DialogTheme" />

<receiver android:name=".tools.BootReceiver">
<receiver android:name=".tools.OnDeleteReceiver"/>

<receiver
android:name=".tools.OnBootReceiver"
android:exported="true">
<intent-filter>
<action android:name="ANDROID.INTENT.ACTION.BOOT_COMPLETED" />
</intent-filter>
Expand Down
162 changes: 162 additions & 0 deletions app/src/main/java/de/dotwee/micropinner/tools/JsonHandler.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
import android.content.Intent;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;

import de.dotwee.micropinner.R;
import de.dotwee.micropinner.ui.MainActivity;

/**
* Created by Lukas on 09.06.2015.
*/
public class BootReceiver extends BroadcastReceiver {
private final static String LOG_TAG = "BootReceiver";
public class OnBootReceiver extends BroadcastReceiver {
private final static String LOG_TAG = "OnBootReceiver";
private final static int DEFAULT_NOTIFICATIONID = 0;

@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "Received boot-intent. Restoring pins...");
new JsonHandler(context).restore();

Intent mainIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
Expand Down
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);
}
}
12 changes: 10 additions & 2 deletions app/src/main/java/de/dotwee/micropinner/ui/EditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import android.widget.Toast;

import de.dotwee.micropinner.R;
import de.dotwee.micropinner.tools.BootReceiver;
import de.dotwee.micropinner.tools.JsonHandler;
import de.dotwee.micropinner.tools.OnBootReceiver;

/**
* Created by Lukas on 09.06.2015.
Expand All @@ -35,7 +36,7 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.dialog_main);

receivedIntent = getIntent();
sendBroadcast(new Intent(this, BootReceiver.class));
sendBroadcast(new Intent(this, OnBootReceiver.class));

notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down Expand Up @@ -95,6 +96,13 @@ void updatePin() {
newContent,
checkBoxPersistentPin.isChecked()
));

new JsonHandler(this).edit(newTitle,
newContent,
receivedIntent.getIntExtra(MainActivity.EXTRA_VISIBILITY, 0),
receivedIntent.getIntExtra(MainActivity.EXTRA_PRIORITY, 0),
checkBoxPersistentPin.isChecked(), receivedIntent.getIntExtra(MainActivity.EXTRA_NOTIFICATION, 1)
);
finish();
}
}
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/de/dotwee/micropinner/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.Random;

import de.dotwee.micropinner.R;
import de.dotwee.micropinner.tools.BootReceiver;
import de.dotwee.micropinner.tools.JsonHandler;
import de.dotwee.micropinner.tools.OnBootReceiver;
import de.dotwee.micropinner.tools.OnDeleteReceiver;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String EXTRA_VISIBILITY = "EXTRA_VISIBILITY", EXTRA_PRIORITY = "EXTRA_PRIORITY", EXTRA_TITLE = "EXTRA_TITLE", EXTRA_CONTENT = "EXTRA_CONTENT", EXTRA_NOTIFICATION = "EXTRA_NOTIFICATION", EXTRA_PERSISTENT = "EXTRA_PERSISTENT";
Expand All @@ -44,6 +46,7 @@ public static Notification generatePin(Context context, int visibility, int prio
.setContentText(content)
.setSmallIcon(R.drawable.ic_star_24dp)
.setPriority(priority)
.setDeleteIntent(PendingIntent.getBroadcast(context, id, new Intent(context, OnDeleteReceiver.class).setAction("notification_cancelled").putExtra(MainActivity.EXTRA_NOTIFICATION, id), PendingIntent.FLAG_CANCEL_CURRENT))
.setOngoing(priority == Notification.PRIORITY_MIN | persistent);

if (Build.VERSION.SDK_INT >= 21) {
Expand All @@ -61,6 +64,7 @@ public static Notification generatePin(Context context, int visibility, int prio
resultIntent.putExtra(EXTRA_PRIORITY, priority);

notification.setContentIntent(PendingIntent.getActivity(context, id, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT));
new JsonHandler(context).append(title, content, visibility, priority, persistent, id);

return notification.build();
}
Expand All @@ -69,6 +73,7 @@ public static Notification generatePin(Context context, int visibility, int prio
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_main);
new JsonHandler(this).restore();

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

Expand Down Expand Up @@ -98,8 +103,8 @@ protected void onCreate(Bundle savedInstanceState) {
// set focus to the title input
editTextTitle.performClick();

// simulate device-boot by sending a new intent to @link BootReceiver.class
sendBroadcast(new Intent(this, BootReceiver.class));
// simulate device-boot by sending a new intent to class OnBootReceiver
sendBroadcast(new Intent(this, OnBootReceiver.class));

// hide advanced stuff
switchAdvancedLayout(false);
Expand Down Expand Up @@ -229,7 +234,7 @@ public void onClick(View v) {

case R.id.checkBoxNewPin:
sharedPreferences.edit().putBoolean(PREF_SHOWNEWPIN, checkBoxShowNewPin.isChecked()).apply();
sendBroadcast(new Intent(this, BootReceiver.class));
sendBroadcast(new Intent(this, OnBootReceiver.class));
break;

case R.id.switchAdvanced:
Expand Down

0 comments on commit 7bee6eb

Please sign in to comment.