Reorganize remote control pieces.

Automatically build jar file for external applications.
Create convenience functions in K9RemoteControl so that external
applications do not need to know the details of handling the Intents
and broadcasts.
Send a permission string in the broadcasts so that unauthorized
applications cannot intercept communication to authorized
applications, such as K-9 Mail.
This commit is contained in:
Daniel Applebaum 2010-01-15 05:02:27 +00:00
parent 6c22507dfb
commit d245e81679
4 changed files with 91 additions and 3 deletions

View File

@ -110,6 +110,9 @@
<property name="main-out-folder" value="../${out-folder}" />
<property name="main-out-classes" value="${main-out-folder}/classes"/>
<property name="rclib" value="${out-folder}/K9RemoteControl.jar" />
<property name="rcdir" value="com/fsck/k9/remotecontrol/**" />
<!-- Intermediate files -->
<property name="dex-file" value="classes.dex" />
<property name="intermediate-dex" value="${out-folder}/${dex-file}" />
@ -201,6 +204,15 @@
</javac>
</target>
<target name="rclib" depends="compile">
<echo>Creating library ${rclib} for remote control
applications</echo>
<jar destfile="${rclib}"
basedir="${out-classes}"
includes="${rcdir}" />
</target>
<!-- Convert this project's .class files into .dex files. -->
<target name="dex" depends="compile">
<echo>Converting compiled files and external libraries into ${out-folder}/${dex-file}...</echo>
@ -247,7 +259,7 @@
<!-- Package the application without signing it.
This allows for the application to be signed later with an official publishing key. -->
<target name="release" depends="dex, package-resources">
<target name="release" depends="rclib, dex, package-resources">
<apkbuilder
outfolder="${out-folder}"
basename="${ant.project.name}"

View File

@ -0,0 +1,33 @@
package com.fsck.k9.remotecontrol;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
class AccountReceiver extends BroadcastReceiver
{
K9AccountReceptor receptor = null;
protected AccountReceiver(K9AccountReceptor nReceptor)
{
receptor = nReceptor;
}
@Override
public void onReceive(Context context, Intent intent)
{
if (K9RemoteControl.K9_REQUEST_ACCOUNTS.equals(intent.getAction()))
{
Bundle bundle = getResultExtras(false);
if (bundle == null)
{
Log.w(K9RemoteControl.LOG_TAG, "Response bundle is empty");
return;
}
receptor.accounts(bundle.getStringArray(K9RemoteControl.K9_ACCOUNT_UUIDS), bundle.getStringArray(K9RemoteControl.K9_ACCOUNT_DESCRIPTIONS));
}
}
}

View File

@ -0,0 +1,11 @@
package com.fsck.k9.remotecontrol;
/**
*
* @author Daniel I. Applebaum
* The interface to implement in order to accept the arrays containing the UUIDs and descriptions of
* the accounts configured in K-9 Mail. Should be passed to fetchAccounts(Context, K9AccountReceptor)
*/
public interface K9AccountReceptor
{
public void accounts(String[] uuids, String[] descriptions);
}

View File

@ -1,8 +1,17 @@
package com.fsck.k9;
package com.fsck.k9.remotecontrol;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* Utillity definitions for Android applications to control the behavior of K-9 Mail. All such applications must declare the following permission:
* <uses-permission android:name="com.fsck.k9.permission.REMOTE_CONTROL"/>
* in their AndroidManifest.xml
* in their AndroidManifest.xml In addition, all applications sending remote control messages to K-9 Mail must
*
* An application that wishes to act on a particular Account in K-9 needs to fetch the list of configured Accounts by broadcasting an
* {@link Intent} using K9_REQUEST_ACCOUNTS as the Action. The broadcast must be made using the {@link ContextWrapper}
@ -15,6 +24,11 @@ package com.fsck.k9;
*/
public class K9RemoteControl
{
/**
* Permission that every application sending a broadcast to K-9 for Remote Control purposes should send on every broadcast.
* Prevent other applications from intercepting the broadcasts.
*/
public final static String K9_REMOTE_CONTROL_PERMISSION = "com.fsck.k9.permission.REMOTE_CONTROL";
/**
* {@link Intent} Action to be sent to K-9 using {@link ContextWrapper.sendOrderedBroadcast} in order to fetch the list of configured Accounts.
* The responseData will contain two String[] with keys K9_ACCOUNT_UUIDS and K9_ACCOUNT_DESCRIPTIONS
@ -105,4 +119,22 @@ public class K9RemoteControl
public final static String K9_THEME_LIGHT = "LIGHT";
public final static String K9_THEME_DARK = "DARK";
protected static String LOG_TAG = "K9RemoteControl";
public static void set(Context context, Intent broadcastIntent)
{
broadcastIntent.setAction(K9RemoteControl.K9_SET);
context.sendBroadcast(broadcastIntent, K9RemoteControl.K9_REMOTE_CONTROL_PERMISSION);
}
public static void fetchAccounts(Context context, K9AccountReceptor receptor)
{
Intent accountFetchIntent = new Intent();
accountFetchIntent.setAction(K9RemoteControl.K9_REQUEST_ACCOUNTS);
AccountReceiver receiver = new AccountReceiver(receptor);
context.sendOrderedBroadcast(accountFetchIntent, K9RemoteControl.K9_REMOTE_CONTROL_PERMISSION, receiver, null, Activity.RESULT_OK, null, null);
}
}