فهرست منبع

Merge pull request #1059 from nextcloud/fixPhonebookIntegration

Fix phonebook integration
Marcel Hibbe 4 سال پیش
والد
کامیت
808d68ecec

+ 2 - 2
app/src/main/java/com/nextcloud/talk/activities/MainActivity.kt

@@ -171,8 +171,8 @@ class MainActivity : BaseActivity(), ActionBarProvider {
 
             when (intent.type) {
                 "vnd.android.cursor.item/vnd.com.nextcloud.talk2.chat" -> {
-                    val user = userId.split("@")[0]
-                    val baseUrl = userId.split("@")[1]
+                    val user = userId.substringBeforeLast("@")
+                    val baseUrl = userId.substringAfterLast("@")
                     if (userUtils.currentUser?.baseUrl?.endsWith(baseUrl) == true) {
                         startConversation(user)
                     } else {

+ 30 - 30
app/src/main/java/com/nextcloud/talk/jobs/ContactAddressBookWorker.kt

@@ -86,45 +86,45 @@ class ContactAddressBookWorker(val context: Context, workerParameters: WorkerPar
             }
         }
 
-        AccountManager.get(context).addAccountExplicitly(Account(ACCOUNT_NAME, ACCOUNT_TYPE), "", null)
+        if(AccountManager.get(context).getAccountsByType(ACCOUNT_TYPE).isEmpty()){
+            AccountManager.get(context).addAccountExplicitly(Account(ACCOUNT_NAME, ACCOUNT_TYPE), "", null)
+        }
 
         // collect all contacts with phone number
         val contactsWithNumbers = collectPhoneNumbers()
 
-        val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0].country
-
-        val map = mutableMapOf<String, Any>()
-        map["location"] = currentLocale
-        map["search"] = contactsWithNumbers
+        if(contactsWithNumbers.isNotEmpty()){
+            val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0].country
 
-        val json = Gson().toJson(map)
+            val map = mutableMapOf<String, Any>()
+            map["location"] = currentLocale
+            map["search"] = contactsWithNumbers
 
-        ncApi.searchContactsByPhoneNumber(
-                ApiUtils.getCredentials(currentUser.username, currentUser.token),
-                ApiUtils.getUrlForSearchByNumber(currentUser.baseUrl),
-                RequestBody.create(MediaType.parse("application/json"), json))
-                .subscribeOn(Schedulers.io())
-                .observeOn(AndroidSchedulers.mainThread())
-                .subscribe(object : Observer<ContactsByNumberOverall> {
-                    override fun onComplete() {
-                    }
+            val json = Gson().toJson(map)
 
-                    override fun onSubscribe(d: Disposable) {
-                    }
+            ncApi.searchContactsByPhoneNumber(
+                    ApiUtils.getCredentials(currentUser.username, currentUser.token),
+                    ApiUtils.getUrlForSearchByNumber(currentUser.baseUrl),
+                    RequestBody.create(MediaType.parse("application/json"), json))
+                    .subscribeOn(Schedulers.io())
+                    .observeOn(AndroidSchedulers.mainThread())
+                    .subscribe(object : Observer<ContactsByNumberOverall> {
+                        override fun onComplete() {
+                        }
 
-                    override fun onNext(foundContacts: ContactsByNumberOverall) {
-                        Log.d(javaClass.simpleName, "next")
+                        override fun onSubscribe(d: Disposable) {
+                        }
 
-                        // todo update
-                        up(foundContacts)
-                    }
+                        override fun onNext(foundContacts: ContactsByNumberOverall) {
+                            up(foundContacts)
+                        }
 
-                    override fun onError(e: Throwable) {
-                        // TODO error handling
-                        Log.d(javaClass.simpleName, "error")
-                    }
+                        override fun onError(e: Throwable) {
+                            Log.e(javaClass.simpleName, "Failed to searchContactsByPhoneNumber", e)
+                        }
 
-                })
+                    })
+        }
 
         // store timestamp 
         appPreferences.setPhoneBookIntegrationLastRun(System.currentTimeMillis())
@@ -321,9 +321,9 @@ class ContactAddressBookWorker(val context: Context, workerParameters: WorkerPar
                 try {
                     context.contentResolver.applyBatch(ContactsContract.AUTHORITY, ops)
                 } catch (e: OperationApplicationException) {
-                    e.printStackTrace()
+                    Log.e(javaClass.simpleName, "", e)
                 } catch (e: RemoteException) {
-                    e.printStackTrace()
+                    Log.e(javaClass.simpleName, "", e)
                 }
             }