Added setting for contacts shortcuts and related improvements

This commit is contained in:
Sylvain Berfini 2019-06-21 14:28:50 +02:00
parent d80891729d
commit 1fb84ad046
10 changed files with 80 additions and 94 deletions

View file

@ -32,7 +32,6 @@ import android.provider.ContactsContract;
import android.view.WindowManager;
import org.linphone.call.CallIncomingActivity;
import org.linphone.call.CallOutgoingActivity;
import org.linphone.compatibility.Compatibility;
import org.linphone.contacts.ContactsManager;
import org.linphone.core.Call;
import org.linphone.core.Call.State;
@ -215,8 +214,6 @@ public final class LinphoneService extends Service {
}
mContactsManager.initializeContactManager();
Compatibility.createChatShortcuts(this);
return START_STICKY;
}

View file

@ -29,18 +29,22 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.linphone.LinphoneManager;
import org.linphone.R;
import org.linphone.contacts.ContactsManager;
import org.linphone.contacts.LinphoneContact;
import org.linphone.core.Address;
import org.linphone.core.ChatRoom;
import org.linphone.core.ChatRoomCapabilities;
import org.linphone.core.tools.Log;
import org.linphone.settings.LinphonePreferences;
import org.linphone.utils.LinphoneShortcutManager;
@TargetApi(25)
class ApiTwentyFivePlus {
public static void createChatShortcuts(Context context) {
if (!context.getResources().getBoolean(R.bool.create_most_recent_chat_rooms_shortcuts))
return;
if (!LinphonePreferences.instance().shortcutsCreationEnabled()) return;
LinphoneShortcutManager manager = new LinphoneShortcutManager(context);
ShortcutManager shortcutManager =
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
@ -64,37 +68,37 @@ class ApiTwentyFivePlus {
}
});
LinphoneShortcutManager manager = new LinphoneShortcutManager(context);
int i = 0;
int maxShortcuts =
min(notEmptyOneToOneRooms.size(), shortcutManager.getMaxShortcutCountPerActivity());
for (int i = 0; i < maxShortcuts; i++) {
ArrayList<LinphoneContact> contacts = new ArrayList<>();
for (ChatRoom room : notEmptyOneToOneRooms) {
// Android can only have around 4-5 shortcuts at a time
ChatRoom room = notEmptyOneToOneRooms.get(i);
ShortcutInfo shortcut = manager.createChatRoomShortcutInfo(room);
if (shortcut != null) {
shortcuts.add(shortcut);
if (i >= maxShortcuts) break;
Address participantAddress =
room.hasCapability(ChatRoomCapabilities.Basic.toInt())
? room.getPeerAddress()
: room.getParticipants()[0].getAddress();
LinphoneContact contact =
ContactsManager.getInstance().findContactFromAddress(participantAddress);
if (contact != null && !contacts.contains(contact)) {
String peerAddress = room.getPeerAddress().asStringUriOnly();
ShortcutInfo shortcut = manager.createChatRoomShortcutInfo(contact, peerAddress);
if (shortcut != null) {
Log.i(
"[Shortcut] Creating launcher shortcut "
+ shortcut.getShortLabel()
+ " for room "
+ shortcut.getId());
shortcuts.add(shortcut);
contacts.add(contact);
i += 1;
}
}
}
shortcutManager.setDynamicShortcuts(shortcuts);
}
public static void updateShortcuts(Context context) {
if (!context.getResources().getBoolean(R.bool.create_most_recent_chat_rooms_shortcuts))
return;
ShortcutManager shortcutManager =
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
LinphoneShortcutManager manager = new LinphoneShortcutManager(context);
for (ShortcutInfo shortcutInfo : shortcutManager.getDynamicShortcuts()) {
ShortcutInfo shortcut = manager.updateShortcutInfo(shortcutInfo);
if (shortcut != null) {
shortcuts.add(shortcut);
}
}
shortcutManager.updateShortcuts(shortcuts);
}
}

View file

@ -251,12 +251,6 @@ public class Compatibility {
}
}
public static void updateShortcuts(Context context) {
if (Version.sdkAboveOrEqual(Version.API25_NOUGAT_71)) {
ApiTwentyFivePlus.updateShortcuts(context);
}
}
public static void enterPipMode(Activity activity) {
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
ApiTwentySixPlus.enterPipMode(activity);

View file

@ -31,6 +31,7 @@ import java.util.HashMap;
import java.util.List;
import org.linphone.LinphoneManager;
import org.linphone.R;
import org.linphone.compatibility.Compatibility;
import org.linphone.core.Core;
import org.linphone.core.Friend;
import org.linphone.core.FriendList;
@ -253,6 +254,8 @@ class AsyncContactsLoader extends AsyncTask<Void, Void, AsyncContactsLoader.Asyn
ContactsManager.getInstance().getContactsListeners()) {
listener.onContactsUpdated();
}
Compatibility.createChatShortcuts(mContext);
Log.i("[Contacts Manager] Synchronization finished");
}

View file

@ -529,6 +529,6 @@ public class ContactsManager extends ContentObserver implements FriendListListen
listener.onContactsUpdated();
}
Compatibility.updateShortcuts(mContext);
Compatibility.createChatShortcuts(mContext);
}
}

View file

@ -31,7 +31,8 @@ public class ContactSettingsFragment extends SettingsFragment {
private View mContactView;
private SwitchSetting mContactPresenceNativeContact,
mFriendListSubscribe,
mDisplayDetailContact;
mDisplayDetailContact,
mCreateShortcuts;
private LinphonePreferences mPrefs;
@Override
@ -55,15 +56,20 @@ public class ContactSettingsFragment extends SettingsFragment {
private void loadSettings() {
mFriendListSubscribe = mContactView.findViewById(R.id.pref_friendlist_subscribe);
mContactPresenceNativeContact =
mContactView.findViewById(R.id.pref_contact_presence_native_contact);
mDisplayDetailContact = mContactView.findViewById(R.id.pref_contact_organization);
mCreateShortcuts = mContactView.findViewById(R.id.pref_contact_shortcuts);
}
private void updateValues() {
setListeners();
mFriendListSubscribe.setChecked(mPrefs.isFriendlistsubscriptionEnabled());
mContactPresenceNativeContact.setChecked(
mPrefs.isPresenceStorageInNativeAndroidContactEnabled());
@ -72,6 +78,12 @@ public class ContactSettingsFragment extends SettingsFragment {
} else {
mDisplayDetailContact.setVisibility(View.INVISIBLE);
}
if (getResources().getBoolean(R.bool.create_most_recent_chat_rooms_shortcuts)) {
mCreateShortcuts.setChecked(mPrefs.shortcutsCreationEnabled());
} else {
mCreateShortcuts.setVisibility(View.INVISIBLE);
}
}
private void setListeners() {
@ -99,6 +111,7 @@ public class ContactSettingsFragment extends SettingsFragment {
}
}
});
mDisplayDetailContact.setListener(
new SettingListenerBase() {
@Override
@ -106,5 +119,13 @@ public class ContactSettingsFragment extends SettingsFragment {
mPrefs.enabledDisplayContactOrganization(newValue);
}
});
mCreateShortcuts.setListener(
new SettingListenerBase() {
@Override
public void onBoolValueChanged(boolean newValue) {
mPrefs.enableChatRoomsShortcuts(newValue);
}
});
}
}

View file

@ -1172,4 +1172,12 @@ public class LinphonePreferences {
public void setVideoPreviewEnabled(boolean enabled) {
getConfig().setBool("app", "video_preview", enabled);
}
public boolean shortcutsCreationEnabled() {
return getConfig().getBool("app", "shortcuts", false);
}
public void enableChatRoomsShortcuts(boolean enable) {
getConfig().setBool("app", "shortcuts", enable);
}
}

View file

@ -29,12 +29,7 @@ import android.util.ArraySet;
import java.util.Set;
import org.linphone.R;
import org.linphone.chat.ChatActivity;
import org.linphone.contacts.ContactsManager;
import org.linphone.contacts.LinphoneContact;
import org.linphone.core.Address;
import org.linphone.core.ChatRoom;
import org.linphone.core.ChatRoomCapabilities;
import org.linphone.core.Factory;
import org.linphone.core.tools.Log;
@TargetApi(25)
@ -48,16 +43,12 @@ public class LinphoneShortcutManager {
mCategories.add(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION);
}
public ShortcutInfo createChatRoomShortcutInfo(ChatRoom room) {
Address peerAddress =
room.hasCapability(ChatRoomCapabilities.Basic.toInt())
? room.getPeerAddress()
: room.getParticipants()[0].getAddress();
LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(peerAddress);
String address = peerAddress.asStringUriOnly();
public ShortcutInfo createChatRoomShortcutInfo(
LinphoneContact contact, String chatRoomAddress) {
if (contact == null) return null;
Bitmap bm = null;
if (contact != null && contact.getThumbnailUri() != null) {
if (contact.getThumbnailUri() != null) {
bm = ImageUtils.getRoundBitmapFromUri(mContext, contact.getThumbnailUri());
}
Icon icon =
@ -65,20 +56,15 @@ public class LinphoneShortcutManager {
? Icon.createWithResource(mContext, R.drawable.avatar)
: Icon.createWithBitmap(bm);
String name =
contact == null
? LinphoneUtils.getAddressDisplayName(peerAddress)
: contact.getFullName();
try {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(mContext, ChatActivity.class);
intent.addFlags(
Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("RemoteSipUri", room.getPeerAddress().asStringUriOnly());
intent.putExtra("RemoteSipUri", chatRoomAddress);
return new ShortcutInfo.Builder(mContext, address)
.setShortLabel(name)
return new ShortcutInfo.Builder(mContext, chatRoomAddress)
.setShortLabel(contact.getFullName())
.setIcon(icon)
.setCategories(mCategories)
.setIntent(intent)
@ -89,38 +75,4 @@ public class LinphoneShortcutManager {
return null;
}
public ShortcutInfo updateShortcutInfo(ShortcutInfo shortcutInfo) {
String address = shortcutInfo.getId();
Address peerAddress = Factory.instance().createAddress(address);
LinphoneContact contact = ContactsManager.getInstance().findContactFromAddress(peerAddress);
if (contact != null) {
Bitmap bm = null;
if (contact != null && contact.getThumbnailUri() != null) {
bm = ImageUtils.getRoundBitmapFromUri(mContext, contact.getThumbnailUri());
}
Icon icon =
bm == null
? Icon.createWithResource(mContext, R.drawable.avatar)
: Icon.createWithBitmap(bm);
String name =
contact == null
? LinphoneUtils.getAddressDisplayName(peerAddress)
: contact.getFullName();
try {
return new ShortcutInfo.Builder(mContext, address)
.setShortLabel(name)
.setIcon(icon)
.setCategories(mCategories)
.setIntent(shortcutInfo.getIntent())
.build();
} catch (Exception e) {
Log.e("[Shortcuts Manager] ShortcutInfo.Builder exception: " + e);
}
}
return shortcutInfo;
}
}

View file

@ -30,6 +30,12 @@
android:layout_height="wrap_content"
linphone:title="@string/pref_contact_display_contact_organization_title" />
<org.linphone.settings.widget.SwitchSetting
android:id="@+id/pref_contact_shortcuts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
linphone:title="@string/pref_create_contact_shortcuts" />
</LinearLayout>
</ScrollView>

View file

@ -435,6 +435,7 @@
<string name="pref_contact_sync_subtitle">Inserting information shortcuts from the Linphone contact into native Android contacts</string>
<string name="pref_contact_title">Contact</string>
<string name="pref_contact_display_contact_organization_title">Display contact organization</string>
<string name="pref_create_contact_shortcuts">Create shortcuts in launcher</string>
<!-- Call settings -->
<string name="pref_call_title">Call</string>