Remove obsolete code from the InstallApk API demo.
1) Remove the "Refresh package" demo. This was using Intent.EXTRA_ALLOW_REPLACE, which was deprecated in API 16. 2) Java code cleanup. Test: Manually. Change-Id: I0cbff934ca223a3fe73a1044ed68f3e079be8237
This commit is contained in:
@@ -38,11 +38,6 @@
|
|||||||
android:text="My Source">
|
android:text="My Source">
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button android:id="@+id/replace"
|
|
||||||
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
|
||||||
android:text="Replace">
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button android:id="@+id/uninstall"
|
<Button android:id="@+id/uninstall"
|
||||||
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
android:text="Uninstall">
|
android:text="Uninstall">
|
||||||
|
|||||||
@@ -21,25 +21,17 @@ package com.example.android.apis.content;
|
|||||||
import com.example.android.apis.R;
|
import com.example.android.apis.R;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentProvider;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ContentProvider.PipeDataWriter;
|
|
||||||
import android.content.res.AssetFileDescriptor;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.ParcelFileDescriptor;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -66,8 +58,6 @@ public class InstallApk extends Activity {
|
|||||||
button.setOnClickListener(mUnknownSourceListener);
|
button.setOnClickListener(mUnknownSourceListener);
|
||||||
button = (Button)findViewById(R.id.my_source);
|
button = (Button)findViewById(R.id.my_source);
|
||||||
button.setOnClickListener(mMySourceListener);
|
button.setOnClickListener(mMySourceListener);
|
||||||
button = (Button)findViewById(R.id.replace);
|
|
||||||
button.setOnClickListener(mReplaceListener);
|
|
||||||
button = (Button)findViewById(R.id.uninstall);
|
button = (Button)findViewById(R.id.uninstall);
|
||||||
button.setOnClickListener(mUninstallListener);
|
button.setOnClickListener(mUninstallListener);
|
||||||
button = (Button)findViewById(R.id.uninstall_result);
|
button = (Button)findViewById(R.id.uninstall_result);
|
||||||
@@ -115,19 +105,6 @@ public class InstallApk extends Activity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private OnClickListener mReplaceListener = new OnClickListener() {
|
|
||||||
public void onClick(View v) {
|
|
||||||
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
||||||
intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));
|
|
||||||
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
|
|
||||||
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
|
|
||||||
intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
|
|
||||||
intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,
|
|
||||||
getApplicationInfo().packageName);
|
|
||||||
startActivityForResult(intent, REQUEST_INSTALL);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private OnClickListener mUninstallListener = new OnClickListener() {
|
private OnClickListener mUninstallListener = new OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
|
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
|
||||||
@@ -150,31 +127,15 @@ public class InstallApk extends Activity {
|
|||||||
private File prepareApk(String assetName) {
|
private File prepareApk(String assetName) {
|
||||||
// Copy the given asset out into a file so that it can be installed.
|
// Copy the given asset out into a file so that it can be installed.
|
||||||
// Returns the path to the file.
|
// Returns the path to the file.
|
||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[16384];
|
||||||
InputStream is = null;
|
try (InputStream is = getAssets().open(assetName);
|
||||||
FileOutputStream fout = null;
|
FileOutputStream fout = openFileOutput("tmp.apk", Context.MODE_WORLD_READABLE)) {
|
||||||
try {
|
|
||||||
is = getAssets().open(assetName);
|
|
||||||
fout = openFileOutput("tmp.apk", Context.MODE_WORLD_READABLE);
|
|
||||||
int n;
|
int n;
|
||||||
while ((n=is.read(buffer)) >= 0) {
|
while ((n=is.read(buffer)) >= 0) {
|
||||||
fout.write(buffer, 0, n);
|
fout.write(buffer, 0, n);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.i("InstallApk", "Failed transferring", e);
|
Log.i("InstallApk", "Failed transferring", e);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (is != null) {
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (fout != null) {
|
|
||||||
fout.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return getFileStreamPath("tmp.apk");
|
return getFileStreamPath("tmp.apk");
|
||||||
|
|||||||
Reference in New Issue
Block a user