Skip to content

Instantly share code, notes, and snippets.

@maggocnx
Created January 15, 2018 14:05
Show Gist options
  • Save maggocnx/60e6d4b9beff8d85e75c01ddb695e910 to your computer and use it in GitHub Desktop.
Save maggocnx/60e6d4b9beff8d85e75c01ddb695e910 to your computer and use it in GitHub Desktop.
package com.gronic.updater;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "GRONIC_UPDATER";
private String mFileName;
private Context mContext;
private boolean mStartAfterInstall;
private TextView mTextView;
private String mAppName;
private String mPackename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter("com.gronic.UPDATE_APP");
mContext = MainActivity.this;
mTextView = (TextView) findViewById(R.id.txtInfo);
mStartAfterInstall = getIntent().getBooleanExtra("start",false);
mFileName = getIntent().getStringExtra("filename");
PackageManager pm = getPackageManager();
mAppName = getAppLabel(pm,mFileName);
mPackename = getPackageManager().getPackageArchiveInfo(mFileName,0).packageName;
mTextView.setText("Installing " + mAppName + "\n" + mPackename);
new InstallTask().execute();
}
public class InstallTask extends AsyncTask{
String mLine;
@Override
protected Object doInBackground(Object[] objects) {
try {
Process p = Runtime.getRuntime().exec( "pm install -r " + mFileName);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
while ((mLine = in.readLine()) != null) {
System.out.println(mLine);
}
in.close();
publishProgress(new String[]{mLine});
in = new BufferedReader (
new InputStreamReader(p.getErrorStream()) );
while ((mLine = in.readLine()) != null) {
System.out.println(mLine);
}
in.close();
p.waitFor();
Log.d(TAG,"Installed ? ");
}
catch (Exception e) {
mLine = "install failed " + e.getMessage();
}
//
return mLine;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
// Toast.makeText(mContext,(String) o, Toast.LENGTH_LONG).show();
if(mStartAfterInstall){
if(isPackageInstalled(mPackename)){
Intent i = getPackageManager().getLaunchIntentForPackage(mPackename);
startActivity(i);
}
}
else{
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
// finishAndRemoveTask();
}
}
@Override
protected void onResume() {
super.onResume();
}
public static String getAppLabel(PackageManager pm, String pathToApk) {
PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);
packageInfo.applicationInfo.sourceDir = pathToApk;
packageInfo.applicationInfo.publicSourceDir = pathToApk;
CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
return label != null ? label.toString() : "";
}
private boolean isPackageInstalled(String packagename) {
try {
getPackageManager().getPackageInfo(packagename, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment