Merge pull request #2088 from deltachat/provider-app-support

provider app support
This commit is contained in:
cyBerta
2021-11-10 14:54:01 +01:00
committed by GitHub
6 changed files with 144 additions and 2 deletions
+10
View File
@@ -295,6 +295,16 @@
<service android:name=".service.GenericForegroundService" />
<service
android:name=".service.IPCAddAccountsService"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="chat.delta.addaccount" />
</intent-filter>
</service>
<receiver android:name=".notifications.MarkReadReceiver"
android:enabled="true"
android:exported="false">
+2 -2
View File
@@ -16,8 +16,8 @@
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp">
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<org.thoughtcrime.securesms.components.emoji.EmojiTextView
android:id="@+id/profile_name"
@@ -59,6 +59,7 @@ import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SEND_SERVER;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SEND_USER;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SERVER_FLAGS;
import static org.thoughtcrime.securesms.connect.DcHelper.getContext;
import static org.thoughtcrime.securesms.service.IPCAddAccountsService.ACCOUNT_DATA;
public class RegistrationActivity extends BaseActionBarActivity implements DcEventCenter.DcEventDelegate {
@@ -175,6 +176,21 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve
int certCheckFlags = DcHelper.getInt(this, "imap_certificate_checks");
certCheck.setSelection(certCheckFlags);
} else if (getIntent() != null && getIntent().getBundleExtra(ACCOUNT_DATA) != null) {
// Companion app might have sent account data
Bundle b = getIntent().getBundleExtra(ACCOUNT_DATA);
String emailAddress = b.getString(CONFIG_ADDRESS);
String password = b.getString(CONFIG_MAIL_PASSWORD);
if (!TextUtils.isEmpty(emailAddress) && !TextUtils.isEmpty(password)) {
emailInput.setText(emailAddress);
passwordInput.setText(password);
onLogin();
} else {
String errorText = "Companion app auto-configuration failed.";
errorText += TextUtils.isEmpty(emailAddress) ? " Missing emailAddress." : "";
errorText += TextUtils.isEmpty(password) ? " Missing password." : "";
Toast.makeText(this, errorText, Toast.LENGTH_LONG).show();
}
}
DcHelper.getEventCenter(this).addObserver(DcContext.DC_EVENT_CONFIGURE_PROGRESS, this);
@@ -52,6 +52,7 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
public static final int PICK_BACKUP = 20574;
private final static String TAG = WelcomeActivity.class.getSimpleName();
public static final String TMP_BACKUP_FILE = "tmp-backup-file";
public static final String DC_REQUEST_ACCOUNT_DATA = "chat.delta.DC_REQUEST_ACCOUNT_DATA";
private boolean manualConfigure = true; // false: configure by QR account creation
private ProgressDialog progressDialog = null;
@@ -77,6 +78,16 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
DcEventCenter eventCenter = DcHelper.getEventCenter(this);
eventCenter.addObserver(DcContext.DC_EVENT_CONFIGURE_PROGRESS, this);
eventCenter.addObserver(DcContext.DC_EVENT_IMEX_PROGRESS, this);
if (!DcHelper.hasAnyConfiguredContext(this)) {
Intent intent = new Intent();
// Since android API 26 only explicit broadcasts are allowed for IPC with a few exceptions.
// As a result we have to send for each companion app we want to support an intent with a
// specified package
intent.setPackage("chat.delta.AndroidYggmail");
intent.setAction(DC_REQUEST_ACCOUNT_DATA);
sendBroadcast(intent);
}
}
@Override
@@ -81,11 +81,23 @@ public class DcHelper {
return ApplicationContext.getInstance(context).notificationCenter;
}
public static boolean hasAnyConfiguredContext(Context context) {
DcAccounts accounts = getAccounts(context);
int[] accountIds = accounts.getAll();
for (int accountId : accountIds) {
if (accounts.getAccount(accountId).isConfigured() == 1) {
return true;
}
}
return false;
}
public static boolean isConfigured(Context context) {
DcContext dcContext = getContext(context);
return dcContext.isConfigured() == 1;
}
public static int getInt(Context context, String key) {
DcContext dcContext = getContext(context);
return dcContext.getConfigInt(key);
@@ -0,0 +1,93 @@
package org.thoughtcrime.securesms.service;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.RegistrationActivity;
import org.thoughtcrime.securesms.connect.AccountManager;
import org.thoughtcrime.securesms.connect.DcHelper;
import java.lang.ref.WeakReference;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_ADDRESS;
import com.b44t.messenger.DcAccounts;
import com.b44t.messenger.DcContext;
/**
* This (interprocess communication) service is invoked by companion apps aiming to add a new account to Delta Chat
*/
public class IPCAddAccountsService extends Service {
public final static int ADD_ACCOUNT = 1;
public final static String ACCOUNT_DATA = "ACCOUNT_DATA";
private static final String TAG = IPCAddAccountsService.class.getSimpleName();
private static class IncomingHandler extends Handler {
final WeakReference<Context> contextRef;
public IncomingHandler(Context context) {
contextRef = new WeakReference<>(context);
}
@Override
public void handleMessage(@NonNull Message msg) {
Bundle data = msg.getData();
Context context = contextRef.get();
if (data != null && context != null && msg.what == ADD_ACCOUNT) {
String newAddress = data.getString(CONFIG_ADDRESS);
Log.d(TAG, "ADD ACCOUNT called for account: " + newAddress);
// check if account already exists, if so, switch to that account
DcAccounts accounts = DcHelper.getAccounts(context);
int[] accountIds = accounts.getAll();
for (int accountId : accountIds) {
DcContext dcContext = accounts.getAccount(accountId);
String accountAddress = dcContext.getConfig(CONFIG_ADDRESS);
if (accountAddress.equals(newAddress)) {
Log.d(TAG, newAddress + " already exists. Switching account.");
AccountManager.getInstance().switchAccount(context, accountId);
Intent switchAccountIntent = new Intent(context, ConversationListActivity.class);
switchAccountIntent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(switchAccountIntent);
return;
}
}
AccountManager.getInstance().beginAccountCreation(context);
Intent registrationIntent = new Intent(context, RegistrationActivity.class);
registrationIntent.putExtra(ACCOUNT_DATA, data);
registrationIntent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(registrationIntent);
}
}
}
private final Messenger messenger = new Messenger(new IncomingHandler(this));
@Nullable
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
}