Allow users to add conversation shortcuts to the home screen.

This commit is contained in:
Leonard Ehrenfried 2018-04-08 12:55:30 +02:00 committed by Greyson Parrelli
parent c7da83a702
commit c3c44e324b
5 changed files with 65 additions and 7 deletions

View File

@ -72,6 +72,7 @@
<!-- For conversation 'shortcuts' on the desktop -->
<uses-permission android:name="android.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- For fixing MMS -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
@ -213,6 +214,7 @@
android:windowSoftInputMode="stateUnchanged"
android:launchMode="singleTask"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
android:exported="true"
android:parentActivityName=".ConversationListActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"

View File

@ -7,4 +7,7 @@
<item android:title="@string/conversation__menu_conversation_settings"
android:id="@+id/menu_conversation_settings"/>
<item android:title="@string/conversation__menu_add_shortcut"
android:id="@+id/menu_add_shortcut"/>
</menu>

View File

@ -135,6 +135,7 @@
<string name="ConversationActivity_group_members">Group members</string>
<string name="ConversationActivity_invalid_recipient">Invalid recipient!</string>
<string name="ConversationActivity_added_to_home_screen">Added to home screen</string>
<string name="ConversationActivity_calls_not_supported">Calls not supported</string>
<string name="ConversationActivity_this_device_does_not_appear_to_support_dial_actions">This device does not appear to support dial actions.</string>
<string name="ConversationActivity_leave_group">Leave group?</string>
@ -313,7 +314,7 @@
<!-- GiphyFragmentPageAdapter -->
<string name="GiphyFragmentPagerAdapter_gifs">GIFs</string>
<string name="GiphyFragmentPagerAdapter_stickers">Stickers</string>
<!-- GroupCreateActivity -->
<string name="GroupCreateActivity_actionbar_title">New group</string>
<string name="GroupCreateActivity_actionbar_edit_title">Edit group</string>
@ -819,7 +820,7 @@
<!-- expiration -->
<string name="expiration_off">Off</string>
<plurals name="expiration_seconds">
<item quantity="one">%d second</item>
<item quantity="other">%d seconds</item>
@ -854,7 +855,7 @@
</plurals>
<string name="expiration_weeks_abbreviated">%dw</string>
<!-- unverified safety numbers -->
<string name="IdentityUtil_unverified_banner_one">Your safety number with %s has changed and is no longer verified</string>
<string name="IdentityUtil_unverified_banner_two">Your safety numbers with %1$s and %2$s are no longer verified</string>
@ -1161,7 +1162,7 @@
<string name="preferences_chats__message_text_size">Message font size</string>
<string name="preferences_events__contact_joined_signal">Contact joined Signal</string>
<string name="preferences_notifications__priority">Priority</string>
<!-- **************************************** -->
<!-- menus -->
<!-- **************************************** -->
@ -1389,6 +1390,7 @@
<string name="prompt_passphrase_activity__signal_is_locked">Signal is locked</string>
<string name="prompt_passphrase_activity__tap_to_unlock">TAP TO UNLOCK</string>
<string name="RegistrationLockDialog_reminder">Reminder:</string>
<string name="conversation__menu_add_shortcut">Add to home screen</string>
<string name="recipient_preferences__about">About</string>
<!-- EOF -->

View File

@ -568,6 +568,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
case R.id.menu_call_secure:
case R.id.menu_call_insecure: handleDial(getRecipient()); return true;
case R.id.menu_view_media: handleViewMedia(); return true;
case R.id.menu_add_shortcut: handleAddShortcut(); return true;
case R.id.menu_add_to_contacts: handleAddToContacts(); return true;
case R.id.menu_reset_secure_session: handleResetSecureSession(); return true;
case R.id.menu_group_recipients: handleDisplayGroupRecipients(); return true;
@ -777,6 +778,37 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
startActivity(intent);
}
private void handleAddShortcut() {
Log.i(TAG, "Creating home screen shortcut for recipient " + recipient.getAddress());
Intent launchIntent = new Intent(getApplicationContext(), ConversationActivity.class);
launchIntent.putExtra(ADDRESS_EXTRA, recipient.getAddress().serialize());
launchIntent.putExtra(TEXT_EXTRA, getIntent().getStringExtra(ConversationActivity.TEXT_EXTRA));
launchIntent.setDataAndType(getIntent().getData(), getIntent().getType());
long existingThread = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(recipient);
launchIntent.putExtra(ConversationActivity.THREAD_ID_EXTRA, existingThread);
launchIntent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, ThreadDatabase.DistributionTypes.DEFAULT);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
final String name = Optional.fromNullable(recipient.getProfileName())
.or(Optional.fromNullable(recipient.getName()))
.or(recipient.toShortString());
// these constants are deprecated but their replacement (ShortcutManager) is available only from API level 25
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
Toast.makeText(this, getString(R.string.ConversationActivity_added_to_home_screen), Toast.LENGTH_LONG).show();
}
private void handleLeavePushGroup() {
if (getRecipient() == null) {
Toast.makeText(this, getString(R.string.ConversationActivity_invalid_recipient),
@ -1348,8 +1380,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private void initializeResources() {
if (recipient != null) recipient.removeListener(this);
recipient = Recipient.from(this, getIntent().getParcelableExtra(ADDRESS_EXTRA), true);
recipient = getRecipientFromExtras(getIntent(), this);
threadId = getIntent().getLongExtra(THREAD_ID_EXTRA, -1);
archived = getIntent().getBooleanExtra(IS_ARCHIVED_EXTRA, false);
distributionType = getIntent().getIntExtra(DISTRIBUTION_TYPE_EXTRA, ThreadDatabase.DistributionTypes.DEFAULT);
@ -1364,6 +1395,26 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
recipient.addListener(this);
}
/**
* Extracts the Recipient instance from the extras contained in the intent.
*
* This can be passed in two ways:
*
* - If the intent was started from inside the app, the address is a parcelable Address instance.
* - If it was launched from the home screen then it is a serialised (stringified) form of the Address, as home screen
* shortcuts cannot contain instances of Address (see BadParcelableException).
*/
static Recipient getRecipientFromExtras(@NonNull Intent intent, @NonNull Context context) {
Address address;
final Address parcelableAddress = intent.getParcelableExtra(ADDRESS_EXTRA);
if(parcelableAddress != null) {
address = parcelableAddress;
} else {
address = Address.fromSerialized((String) intent.getExtras().get(ADDRESS_EXTRA));
}
return Recipient.from(context, address, true);
}
private void initializeProfiles() {
if (!isSecureText) {
Log.i(TAG, "SMS contact, no profile fetch needed.");

View File

@ -206,7 +206,7 @@ public class ConversationFragment extends Fragment
}
private void initializeResources() {
this.recipient = Recipient.from(getActivity(), getActivity().getIntent().getParcelableExtra(ConversationActivity.ADDRESS_EXTRA), true);
this.recipient = ConversationActivity.getRecipientFromExtras(getActivity().getIntent(), getActivity());
this.threadId = this.getActivity().getIntent().getLongExtra(ConversationActivity.THREAD_ID_EXTRA, -1);
this.lastSeen = this.getActivity().getIntent().getLongExtra(ConversationActivity.LAST_SEEN_EXTRA, -1);
this.startingPosition = this.getActivity().getIntent().getIntExtra(ConversationActivity.STARTING_POSITION_EXTRA, -1);