Allow users to add conversation shortcuts to the home screen.
This commit is contained in:
parent
c7da83a702
commit
c3c44e324b
5 changed files with 65 additions and 7 deletions
|
@ -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"
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
@ -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 -->
|
||||
|
||||
|
|
|
@ -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.");
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue