Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete + unregister App #150

Merged
merged 6 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/src/main/java/com/github/gotify/messages/MessagesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
import com.github.gotify.Utils;
import com.github.gotify.api.Api;
import com.github.gotify.api.ApiException;
import com.github.gotify.api.Callback;
import com.github.gotify.api.ClientFactory;
import com.github.gotify.client.ApiClient;
import com.github.gotify.client.api.ApplicationApi;
import com.github.gotify.client.api.ClientApi;
import com.github.gotify.client.api.MessageApi;
import com.github.gotify.client.model.Application;
Expand All @@ -60,6 +62,7 @@
import com.github.gotify.messages.provider.MessageState;
import com.github.gotify.messages.provider.MessageWithImage;
import com.github.gotify.picasso.PicassoHandler;
import com.github.gotify.service.MessagingDatabase;
import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.SettingsActivity;
import com.github.gotify.sharing.ShareActivity;
Expand Down Expand Up @@ -171,6 +174,7 @@ public void onDrawerClosed(View drawerView) {
new SelectApplicationAndUpdateMessages(true)
.execute(selectAppIdOnDrawerClose);
selectAppIdOnDrawerClose = null;
invalidateOptionsMenu();
}
}
});
Expand All @@ -192,6 +196,10 @@ public void onDrawerClosed(View drawerView) {
}

public void onRefreshAll(View view) {
refreshAll();
}

public void refreshAll() {
try {
picassoHandler.evict();
} catch (IOException e) {
Expand Down Expand Up @@ -560,6 +568,7 @@ protected void onPostExecute(Boolean update) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.messages_action, menu);
menu.findItem(R.id.action_delete_app).setVisible(appId != MessageState.ALL_MESSAGES);
return super.onCreateOptionsMenu(menu);
}

Expand All @@ -568,9 +577,36 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_delete_all) {
new DeleteMessages().execute(appId);
}
if (item.getItemId() == R.id.action_delete_app) {
android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(this);
alert.setTitle(R.string.delete_app);
alert.setMessage(R.string.ack);
alert.setPositiveButton(R.string.yes, (dialog, which) -> deleteApp(appId));
alert.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss());
alert.show();
}
return super.onContextItemSelected(item);
}

private void deleteApp(Long appId) {
MessagingDatabase db = new MessagingDatabase(this);
db.forceUnregisterApp(appId);
db.close();

ApiClient client =
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token());

client.createService(ApplicationApi.class)
.deleteApp(appId)
.enqueue(
Callback.callInUI(
this,
(ignored) -> refreshAll(),
(e) ->
Utils.showSnackBar(
this, getString(R.string.error_delete_app))));
}

private class LoadMore extends AsyncTask<Long, Void, List<MessageWithImage>> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n
db.delete(TABLE_APPS,selection,selectionArgs)
}

fun forceUnregisterApp(packageName: String){
fun forceUnregisterApp(appId: Long){
val db = writableDatabase
val selection = "$FIELD_PACKAGE_NAME = ?"
val selectionArgs = arrayOf(packageName)
val selection = "$FIELD_APP_ID = ?"
val selectionArgs = arrayOf(appId.toString())
db.delete(TABLE_APPS,selection,selectionArgs)
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/messages_action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
android:title="@string/delete_all"
android:id="@+id/action_delete_all"
android:orderInCategory="100"/>
<item
android:title="@string/delete_app"
android:id="@+id/action_delete_app"
android:orderInCategory="101"/>
</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<string name="websocket_closed_try_reconnect">Connection closed, trying to establish a new one.</string>
<string name="grouped_message">Received %d messages while being disconnected</string>
<string name="delete_all">Delete all</string>
<string name="delete_app">Delete this application</string>
<string name="error_delete_app">Could not delete this app</string>
<string name="delete_logs">Delete logs</string>
<string name="copy_logs">Copy logs</string>
<string name="logs_copied">Logs copied</string>
Expand All @@ -48,6 +50,8 @@
<string name="refresh_all">Refresh all</string>
<string name="logout_confirm">Do you really want to logout?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="ack">Are you sure?</string>
<string name="missed_messages">Missed messages</string>
<string name="grouped_notification_text">New Messages</string>
<string name="websocket_listening">Listening to %s</string>
Expand Down