Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.content;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import com.example.android.apis.R;
     22 
     23 import android.app.Activity;
     24 import android.content.ContentProvider;
     25 import android.content.ContentValues;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.ContentProvider.PipeDataWriter;
     29 import android.content.res.AssetFileDescriptor;
     30 import android.database.Cursor;
     31 import android.net.Uri;
     32 import android.os.Bundle;
     33 import android.os.ParcelFileDescriptor;
     34 import android.util.Log;
     35 import android.view.View;
     36 import android.view.View.OnClickListener;
     37 import android.widget.Button;
     38 import android.widget.TextView;
     39 import android.widget.Toast;
     40 
     41 import java.io.File;
     42 import java.io.FileNotFoundException;
     43 import java.io.FileOutputStream;
     44 import java.io.IOException;
     45 import java.io.InputStream;
     46 
     47 
     48 /**
     49  * Demonstration of styled text resources.
     50  */
     51 public class InstallApk extends Activity {
     52     static final int REQUEST_INSTALL = 1;
     53     static final int REQUEST_UNINSTALL = 2;
     54 
     55     @Override
     56     protected void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58 
     59         setContentView(R.layout.install_apk);
     60 
     61         // Watch for button clicks.
     62         Button button = (Button)findViewById(R.id.unknown_source);
     63         button.setOnClickListener(mUnknownSourceListener);
     64         button = (Button)findViewById(R.id.my_source);
     65         button.setOnClickListener(mMySourceListener);
     66         button = (Button)findViewById(R.id.replace);
     67         button.setOnClickListener(mReplaceListener);
     68         button = (Button)findViewById(R.id.uninstall);
     69         button.setOnClickListener(mUninstallListener);
     70         button = (Button)findViewById(R.id.uninstall_result);
     71         button.setOnClickListener(mUninstallResultListener);
     72     }
     73 
     74     @Override
     75     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
     76         if (requestCode == REQUEST_INSTALL) {
     77             if (resultCode == Activity.RESULT_OK) {
     78                 Toast.makeText(this, "Install succeeded!", Toast.LENGTH_SHORT).show();
     79             } else if (resultCode == Activity.RESULT_CANCELED) {
     80                 Toast.makeText(this, "Install canceled!", Toast.LENGTH_SHORT).show();
     81             } else {
     82                 Toast.makeText(this, "Install Failed!", Toast.LENGTH_SHORT).show();
     83             }
     84         } else if (requestCode == REQUEST_UNINSTALL) {
     85             if (resultCode == Activity.RESULT_OK) {
     86                 Toast.makeText(this, "Uninstall succeeded!", Toast.LENGTH_SHORT).show();
     87             } else if (resultCode == Activity.RESULT_CANCELED) {
     88                 Toast.makeText(this, "Uninstall canceled!", Toast.LENGTH_SHORT).show();
     89             } else {
     90                 Toast.makeText(this, "Uninstall Failed!", Toast.LENGTH_SHORT).show();
     91             }
     92         }
     93     }
     94 
     95     private OnClickListener mUnknownSourceListener = new OnClickListener() {
     96         public void onClick(View v) {
     97             Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
     98             intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));
     99             startActivity(intent);
    100         }
    101     };
    102 
    103     private OnClickListener mMySourceListener = new OnClickListener() {
    104         public void onClick(View v) {
    105             Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    106             intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));
    107             intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
    108             intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
    109             intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,
    110                     getApplicationInfo().packageName);
    111             startActivityForResult(intent, REQUEST_INSTALL);
    112         }
    113     };
    114 
    115     private OnClickListener mReplaceListener = new OnClickListener() {
    116         public void onClick(View v) {
    117             Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    118             intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));
    119             intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
    120             intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
    121             intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
    122             intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,
    123                     getApplicationInfo().packageName);
    124             startActivityForResult(intent, REQUEST_INSTALL);
    125         }
    126     };
    127 
    128     private OnClickListener mUninstallListener = new OnClickListener() {
    129         public void onClick(View v) {
    130             Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
    131             intent.setData(Uri.parse(
    132                     "package:com.example.android.helloactivity"));
    133             startActivity(intent);
    134         }
    135     };
    136 
    137     private OnClickListener mUninstallResultListener = new OnClickListener() {
    138         public void onClick(View v) {
    139             Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
    140             intent.setData(Uri.parse(
    141                     "package:com.example.android.helloactivity"));
    142             intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
    143             startActivityForResult(intent, REQUEST_UNINSTALL);
    144         }
    145     };
    146 
    147     private File prepareApk(String assetName) {
    148         // Copy the given asset out into a file so that it can be installed.
    149         // Returns the path to the file.
    150         byte[] buffer = new byte[8192];
    151         InputStream is = null;
    152         FileOutputStream fout = null;
    153         try {
    154             is = getAssets().open(assetName);
    155             fout = openFileOutput("tmp.apk", Context.MODE_WORLD_READABLE);
    156             int n;
    157             while ((n=is.read(buffer)) >= 0) {
    158                 fout.write(buffer, 0, n);
    159             }
    160         } catch (IOException e) {
    161             Log.i("InstallApk", "Failed transferring", e);
    162         } finally {
    163             try {
    164                 if (is != null) {
    165                     is.close();
    166                 }
    167             } catch (IOException e) {
    168             }
    169             try {
    170                 if (fout != null) {
    171                     fout.close();
    172                 }
    173             } catch (IOException e) {
    174             }
    175         }
    176 
    177         return getFileStreamPath("tmp.apk");
    178     }
    179 }
    180