mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
option to add webxdc apps to home screen (#2353)
* add 'Add to Home Screen' menus for webxdc apps * make adding shortcut basically work * avoid starting webxdc from foreign accounts * recreate back stack, if possible according to https://developer.android.com/training/notify-user/navigation : "When you start an activity from a notification, you must preserve the user's expected navigation experience. Tapping Back should take the user back through the app's normal work flow to the Home screen [...]" same seems to be true for shortcuts using the same class for constructing the back stack. the back stack is reconstructed for API 26 (Oreo, 2017) or newer, for oder API `requestPinShortcut()` only uses the top-level activity for the shortcut. working around that is probably possible, but would be quite some effort in an already complicated area and would result in bugs that are not so easy to see. also, on these old androids, users are probably used to the no-back-stack behaviour. * show a warning if app was deleted * add 'Add to Home Screen' also to gallery beside chat- and webxdc-activity, this is the third place where the webxdc is listed. as adding may result in dialogs from the launcher, we allow the option only for a single selected webxdc, as otherwise a series of dialogs would pop up. * show 'Done' toast short, this is what we are doing at all the other places when showing 'Done'
This commit is contained in:
+3
-1
@@ -30,6 +30,7 @@
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.INSTALL_SHORTCUT" />
|
||||
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.RAISED_THREAD_PRIORITY" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
@@ -286,7 +287,8 @@
|
||||
<activity android:name=".WebxdcActivity"
|
||||
android:label=""
|
||||
android:theme="@style/TextSecure.LightTheme"
|
||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize">
|
||||
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
|
||||
android:exported="true">
|
||||
</activity>
|
||||
|
||||
<activity android:name=".FullMsgActivity"
|
||||
|
||||
@@ -20,6 +20,11 @@
|
||||
android:icon="@drawable/ic_share_white_24dp"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item android:title="@string/add_to_home_screen"
|
||||
android:id="@+id/menu_add_to_home_screen"
|
||||
android:visible="false"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item android:title="@string/menu_copy_to_clipboard"
|
||||
android:id="@+id/menu_context_copy"
|
||||
android:icon="?menu_copy_icon"
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
android:icon="@drawable/ic_share_white_24dp"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item android:id="@+id/menu_add_to_home_screen"
|
||||
android:title="@string/add_to_home_screen"
|
||||
android:visible="false"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item android:id="@+id/show_in_chat"
|
||||
android:title="@string/show_in_chat"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/add_to_home_screen"
|
||||
android:id="@+id/menu_add_to_home_screen" />
|
||||
<item android:title="@string/source_code"
|
||||
android:id="@+id/source_code" />
|
||||
|
||||
|
||||
@@ -264,6 +264,8 @@
|
||||
<string name="ConversationFragment_quoted_message_not_found">Original message not found</string>
|
||||
<string name="reply_privately">Reply Privately</string>
|
||||
<string name="source_code">Source Code</string>
|
||||
<!-- Menu item beside an app/chat that adds an icon to the system's home screen. If the user taps that icon, the app/chat is opened directly. -->
|
||||
<string name="add_to_home_screen">Add to Home Screen</string>
|
||||
|
||||
<string name="mute_for_one_hour">Mute for 1 hour</string>
|
||||
<string name="mute_for_two_hours">Mute for 2 hours</string>
|
||||
|
||||
@@ -52,6 +52,10 @@ public class DcMsg {
|
||||
this.msgCPtr = msgCPtr;
|
||||
}
|
||||
|
||||
public boolean isOk() {
|
||||
return msgCPtr != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
|
||||
@@ -320,6 +320,7 @@ public class ConversationFragment extends MessageSelectorFragment
|
||||
menu.findItem(R.id.menu_context_share).setVisible(false);
|
||||
menu.findItem(R.id.menu_context_reply).setVisible(false);
|
||||
menu.findItem(R.id.menu_context_reply_privately).setVisible(false);
|
||||
menu.findItem(R.id.menu_add_to_home_screen).setVisible(false);
|
||||
} else {
|
||||
DcMsg messageRecord = messageRecords.iterator().next();
|
||||
DcChat chat = getListAdapter().getChat();
|
||||
@@ -329,6 +330,7 @@ public class ConversationFragment extends MessageSelectorFragment
|
||||
menu.findItem(R.id.menu_context_reply).setVisible(chat.canSend() && canReply);
|
||||
boolean showReplyPrivately = chat.isMultiUser() && !messageRecord.isOutgoing() && canReply;
|
||||
menu.findItem(R.id.menu_context_reply_privately).setVisible(showReplyPrivately);
|
||||
menu.findItem(R.id.menu_add_to_home_screen).setVisible(messageRecord.getType() == DcMsg.DC_MSG_WEBXDC);
|
||||
}
|
||||
|
||||
// if one of the selected items cannot be saved, disable saving.
|
||||
@@ -926,6 +928,10 @@ public class ConversationFragment extends MessageSelectorFragment
|
||||
handleForwardMessage(getListAdapter().getSelectedItems());
|
||||
actionMode.finish();
|
||||
return true;
|
||||
case R.id.menu_add_to_home_screen:
|
||||
WebxdcActivity.addToHomeScreen(getActivity(), getSelectedMessageRecord(getListAdapter().getSelectedItems()).getId());
|
||||
actionMode.finish();
|
||||
return true;
|
||||
case R.id.menu_context_save_attachment:
|
||||
handleSaveAttachment(getListAdapter().getSelectedItems());
|
||||
return true;
|
||||
|
||||
@@ -182,6 +182,9 @@ public class ProfileDocumentsFragment
|
||||
menu.findItem(R.id.details).setVisible(singleSelection);
|
||||
menu.findItem(R.id.show_in_chat).setVisible(singleSelection);
|
||||
menu.findItem(R.id.share).setVisible(singleSelection);
|
||||
|
||||
boolean webxdcApp = singleSelection && messageRecords.iterator().next().getType() == DcMsg.DC_MSG_WEBXDC;
|
||||
menu.findItem(R.id.menu_add_to_home_screen).setVisible(webxdcApp);
|
||||
}
|
||||
|
||||
private ProfileDocumentsAdapter getListAdapter() {
|
||||
@@ -225,6 +228,10 @@ public class ProfileDocumentsFragment
|
||||
case R.id.share:
|
||||
handleShare(getSelectedMessageRecord(getListAdapter().getSelectedMedia()));
|
||||
return true;
|
||||
case R.id.menu_add_to_home_screen:
|
||||
WebxdcActivity.addToHomeScreen(getActivity(), getSelectedMessageRecord(getListAdapter().getSelectedMedia()).getId());
|
||||
mode.finish();
|
||||
return true;
|
||||
case R.id.show_in_chat:
|
||||
handleShowInChat(getSelectedMessageRecord(getListAdapter().getSelectedMedia()));
|
||||
return true;
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package org.thoughtcrime.securesms;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
@@ -17,6 +21,10 @@ import android.webkit.WebView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.TaskStackBuilder;
|
||||
import androidx.core.content.pm.ShortcutInfoCompat;
|
||||
import androidx.core.content.pm.ShortcutManagerCompat;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
|
||||
import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcContext;
|
||||
@@ -41,20 +49,43 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
|
||||
private String baseURL;
|
||||
private String sourceCodeUrl = "";
|
||||
|
||||
|
||||
|
||||
public static void openWebxdcActivity(Context context, DcMsg instance) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (Prefs.isDeveloperModeEnabled(context)) {
|
||||
WebView.setWebContentsDebuggingEnabled(true);
|
||||
}
|
||||
|
||||
Intent intent =new Intent(context, WebxdcActivity.class);
|
||||
intent.putExtra("appMessageId", instance.getId());
|
||||
context.startActivity(intent);
|
||||
context.startActivity(getWebxdcIntent(context, instance.getId()));
|
||||
} else {
|
||||
Toast.makeText(context, "At least Android 5.0 (Lollipop) required for Webxdc.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
private static Intent getWebxdcIntent(Context context, int msgId) {
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
Intent intent = new Intent(context, WebxdcActivity.class);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
intent.putExtra("accountId", dcContext.getAccountId());
|
||||
intent.putExtra("appMessageId", msgId);
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static Intent[] getWebxdcIntentWithParentStack(Context context, int msgId) {
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
|
||||
final Intent chatIntent = new Intent(context, ConversationActivity.class)
|
||||
.putExtra(ConversationActivity.CHAT_ID_EXTRA, dcContext.getMsg(msgId).getChatId())
|
||||
.setAction(Intent.ACTION_VIEW);
|
||||
|
||||
final Intent webxdcIntent = getWebxdcIntent(context, msgId);
|
||||
|
||||
return TaskStackBuilder.create(context)
|
||||
.addNextIntentWithParentStack(chatIntent)
|
||||
.addNextIntent(webxdcIntent)
|
||||
.getIntents();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle state, boolean ready) {
|
||||
super.onCreate(state, ready);
|
||||
@@ -65,7 +96,19 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
|
||||
int appMessageId = b.getInt("appMessageId");
|
||||
|
||||
this.dcContext = DcHelper.getContext(getApplicationContext());
|
||||
if (dcContext.getAccountId() != b.getInt("accountId")) {
|
||||
Toast.makeText(this, "Switch to belonging account first.", Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
this.dcAppMsg = this.dcContext.getMsg(appMessageId);
|
||||
if (!this.dcAppMsg.isOk()) {
|
||||
Toast.makeText(this, "Webxdc does no longer exist.", Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
// `msg_id` in the subdomain makes sure, different apps using same files do not share the same cache entry
|
||||
// (WebView may use a global cache shared across objects).
|
||||
// (a random-id would also work, but would need maintenance and does not add benefits as we regard the file-part interceptRequest() only,
|
||||
@@ -107,6 +150,9 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
super.onOptionsItemSelected(item);
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_add_to_home_screen:
|
||||
addToHomeScreen(this, dcAppMsg.getId());
|
||||
return true;
|
||||
case R.id.source_code:
|
||||
openUrlInBrowser(this, sourceCodeUrl);
|
||||
return true;
|
||||
@@ -192,6 +238,36 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
|
||||
});
|
||||
}
|
||||
|
||||
public static void addToHomeScreen(Activity activity, int msgId) {
|
||||
Context context = activity.getApplicationContext();
|
||||
try {
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
DcMsg msg = dcContext.getMsg(msgId);
|
||||
final JSONObject info = msg.getWebxdcInfo();
|
||||
|
||||
final String docName = JsonUtils.optString(info, "document");
|
||||
final String xdcName = JsonUtils.optString(info, "name");
|
||||
byte[] blob = msg.getWebxdcBlob(JsonUtils.optString(info, "icon"));
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(blob);
|
||||
BitmapDrawable drawable = (BitmapDrawable) Drawable.createFromStream(is, "icon");
|
||||
Bitmap bitmap = drawable.getBitmap();
|
||||
|
||||
ShortcutInfoCompat shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, "xdc-" + dcContext.getAccountId() + "-" + msgId)
|
||||
.setShortLabel(docName.isEmpty() ? xdcName : docName)
|
||||
.setIcon(IconCompat.createWithBitmap(bitmap))
|
||||
.setIntents(getWebxdcIntentWithParentStack(context, msgId))
|
||||
.build();
|
||||
|
||||
if (ShortcutManagerCompat.requestPinShortcut(context, shortcutInfoCompat, null)) {
|
||||
Toast.makeText(context, R.string.done, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(context, "ErrAddToHomescreen: requestPinShortcut() failed", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} catch(Exception e) {
|
||||
Toast.makeText(context, "ErrAddToHomescreen: " + e, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
class InternalJSApi {
|
||||
@JavascriptInterface
|
||||
public String selfAddr() {
|
||||
|
||||
@@ -24,8 +24,9 @@ public class ConversationAdaptiveActionsToolbar extends Toolbar {
|
||||
private static final int OVERFLOW_VIEW_WIDTH_DP = 36;
|
||||
|
||||
private static final int ID_NEVER_SHOW_AS_ACTION_1 = R.id.menu_context_reply_privately;
|
||||
private static final int ID_NEVER_SHOW_AS_ACTION_2 = R.id.menu_context_save_attachment;
|
||||
private static final int ID_NEVER_SHOW_AS_ACTION_3 = R.id.menu_resend;
|
||||
private static final int ID_NEVER_SHOW_AS_ACTION_2 = R.id.menu_add_to_home_screen;
|
||||
private static final int ID_NEVER_SHOW_AS_ACTION_3 = R.id.menu_context_save_attachment;
|
||||
private static final int ID_NEVER_SHOW_AS_ACTION_4 = R.id.menu_resend;
|
||||
private static final int ID_ALWAYS_SHOW_AS_ACTION = R.id.menu_context_forward;
|
||||
|
||||
private int maxShown;
|
||||
@@ -81,7 +82,8 @@ public class ConversationAdaptiveActionsToolbar extends Toolbar {
|
||||
|
||||
boolean neverShowAsAction = item.getItemId() == ID_NEVER_SHOW_AS_ACTION_1
|
||||
|| item.getItemId() == ID_NEVER_SHOW_AS_ACTION_2
|
||||
|| item.getItemId() == ID_NEVER_SHOW_AS_ACTION_3;
|
||||
|| item.getItemId() == ID_NEVER_SHOW_AS_ACTION_3
|
||||
|| item.getItemId() == ID_NEVER_SHOW_AS_ACTION_4;
|
||||
boolean alwaysShowAsAction = item.getItemId() == ID_ALWAYS_SHOW_AS_ACTION;
|
||||
|
||||
if (alwaysShowAsAction) continue;
|
||||
|
||||
Reference in New Issue
Block a user