enable sending installed APKs via NFC/Android Beam on AppDetails

If you are viewing the AppDetails screen for an installed app, this code
configures Android Beam to send the APK for that installed app if the you
initiate via NFC.

Also move the SDK checks into each method so that they are easier to use
without doing the wrong thing.
This commit is contained in:
Hans-Christoph Steiner 2014-04-20 21:58:08 -04:00
parent 52e0f373af
commit 0db711c08d
4 changed files with 60 additions and 32 deletions

View File

@ -34,11 +34,13 @@ import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.Signature;
@ -624,11 +626,14 @@ public class AppDetails extends ListActivity {
adapter.notifyDataSetChanged();
TextView tv = (TextView) findViewById(R.id.status);
if (!app.isInstalled())
tv.setText(getString(R.string.details_notinstalled));
else
if (app.isInstalled()) {
tv.setText(getString(R.string.details_installed,
app.installedVersionName));
NfcBeamManager.setAndroidBeam(this, app.id);
} else {
tv.setText(getString(R.string.details_notinstalled));
NfcBeamManager.disableAndroidBeam(this);
}
tv = (TextView) infoView.findViewById(R.id.signature);
if (pref_expert && mInstalledSignature != null) {
@ -1101,5 +1106,4 @@ public class AppDetails extends ListActivity {
break;
}
}
}

View File

@ -129,9 +129,8 @@ public class FDroid extends FragmentActivity {
@Override
protected void onResume() {
super.onResume();
// RepoDetailsActivity sets a different beam, so reset here
if (Build.VERSION.SDK_INT >= 16)
setupAndroidBeam();
// AppDetails and RepoDetailsActivity set different NFC actions, so reset here
NfcBeamManager.setAndroidBeam(this, getApplication().getPackageName());
}
@Override
@ -422,23 +421,4 @@ public class FDroid extends FragmentActivity {
}
@TargetApi(16)
private void setupAndroidBeam() {
PackageManager pm = getPackageManager();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter != null) {
ApplicationInfo appInfo;
try {
appInfo = pm.getApplicationInfo("org.fdroid.fdroid",
PackageManager.GET_META_DATA);
// TODO can we send the repo here also, as a file?
Uri uris[] = {
Uri.parse("file://" + appInfo.publicSourceDir),
};
nfcAdapter.setBeamPushUris(uris, this);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,43 @@
package org.fdroid.fdroid;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Build;
@TargetApi(16)
public class NfcBeamManager {
static void setAndroidBeam(Activity activity, String packageName) {
if (Build.VERSION.SDK_INT < 16)
return;
PackageManager pm = activity.getPackageManager();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdapter != null) {
ApplicationInfo appInfo;
try {
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
Uri uris[] = {
Uri.parse("file://" + appInfo.publicSourceDir),
};
nfcAdapter.setBeamPushUris(uris, activity);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
static void disableAndroidBeam(Activity activity) {
if (Build.VERSION.SDK_INT < 16)
return;
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdapter != null)
nfcAdapter.setBeamPushUris(null, activity);
}
}

View File

@ -69,14 +69,12 @@ public class RepoDetailsActivity extends FragmentActivity {
setTitle(repo.getName());
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
// required NFC support starts in android-14
if (Build.VERSION.SDK_INT >= 14)
setNfc();
}
@TargetApi(14)
private void setNfc() {
if (Build.VERSION.SDK_INT < 14)
return;
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
return;
@ -97,8 +95,9 @@ public class RepoDetailsActivity extends FragmentActivity {
public void onResume() {
Log.i(TAG, "onResume");
super.onResume();
if (Build.VERSION.SDK_INT >= 9)
processIntent(getIntent());
// FDroid.java and AppDetails set different NFC actions, so reset here
setNfc();
processIntent(getIntent());
}
@Override
@ -112,6 +111,8 @@ public class RepoDetailsActivity extends FragmentActivity {
@TargetApi(9)
void processIntent(Intent i) {
if (Build.VERSION.SDK_INT < 9)
return;
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
Log.i(TAG, "ACTION_NDEF_DISCOVERED");
Parcelable[] rawMsgs =