Install privileged methods

This commit is contained in:
Dominik Schürmann 2015-08-27 11:00:40 +02:00
parent 5bfc30651a
commit 1fd85f40e7
7 changed files with 68 additions and 34 deletions

View File

@ -296,23 +296,23 @@
<string name="install_error_cannot_parse">An error occurred while parsing the package</string>
<string name="uninstall_error_title">Uninstall error</string>
<string name="uninstall_error_unknown">Failed to uninstall due to an unknown error</string>
<string name="system_permission_denied_title">System permissions denied</string>
<string name="system_permission_denied_body">This option is only available when F-Droid is installed as a privileged app.</string>
<string name="system_permission_install_via_root">Install as a privileged app</string>
<string name="system_permission_denied_title">Privileged F-Droid is not available</string>
<string name="system_permission_denied_body">This option is only available when privileged F-Droid is installed.</string>
<string name="system_permission_install_via_root">Install privileged F-Droid</string>
<string name="system_install_first_time_notification">Install privileged F-Droid?</string>
<string name="system_install_first_time_notification_message_short">Touch for more information.</string>
<string name="system_install_first_time_notification_message">Touch to install F-Droid as a privileged app, tightly coupled with the Android operating system. This allows F-Droid to install, upgrade and uninstall apps on its own.\nYou can also do this later from F-Droid\'s preferences.</string>
<string name="system_install_post_success">Successful installation as a privileged app</string>
<string name="system_install_post_fail">Installation as a privileged app failed</string>
<string name="system_install_post_success_message">F-Droid has been successfully installed as a privileged app. This allows F-Droid to install, upgrade and uninstall apps on its own.</string>
<string name="system_install_post_fail_message">The installation of F-Droid as a privileged app failed. The installation method is not supported by all Android distributions, please consult the F-Droid bug tracker for more information.</string>
<string name="system_install_first_time_notification_message">Touch to install privileged F-Droid, tightly coupled with the Android operating system. This allows F-Droid to install, upgrade and uninstall apps on its own.\nYou can also do this later from F-Droid\'s preferences.</string>
<string name="system_install_post_success">Successfully installed privileged F-Droid</string>
<string name="system_install_post_fail">Installation of privileged F-Droid failed</string>
<string name="system_install_post_success_message">Privileged F-Droid has been successfully installed. This allows F-Droid to install, upgrade and uninstall apps on its own.</string>
<string name="system_install_post_fail_message">The installation of privileged F-Droid has failed. The installation method is not supported by all Android distributions, please consult the F-Droid bug tracker for more information.</string>
<string name="system_install_installing">installing…</string>
<string name="system_install_uninstalling">uninstalling…</string>
<string name="system_install_question">Do you want to install F-Droid as a privileged app?\nThis takes up to 10 seconds.</string>
<string name="system_install_question_lollipop">Do you want to install F-Droid as a privileged app?\nThis takes up to 10 seconds and the device will be &lt;b>rebooted&lt;/b> afterwards.</string>
<string name="system_install_first_time_message">Looks like you have root access on your device. You can now install F-Droid as a privileged app, tightly coupled with the Android operating system. This allows F-Droid to install, upgrade and uninstall apps on its own.</string>
<string name="system_uninstall">Do you want to uninstall F-Droid?</string>
<string name="system_uninstall_message">This will uninstall F-Droid completely.</string>
<string name="system_install_question">Do you want to install privileged F-Droid?\nThis takes up to 10 seconds.</string>
<string name="system_install_question_lollipop">Do you want to install privileged F-Droid?\nThis takes up to 10 seconds and the device will be &lt;b>rebooted&lt;/b> afterwards.</string>
<string name="system_install_first_time_message">Looks like you have root access on your device. You can now install privileged F-Droid, tightly coupled with the Android operating system. This allows F-Droid to install, upgrade and uninstall apps on its own.</string>
<string name="system_uninstall">Do you want to uninstall privileged F-Droid?</string>
<string name="system_uninstall_message">This will uninstall privileged F-Droid.</string>
<string name="system_uninstall_button">Uninstall</string>
<string name="app_description">F-Droid is an installable catalogue of FOSS (Free and Open Source Software) applications for the Android platform. The client makes it easy to browse, install, and keep track of updates on your device.</string>

View File

@ -870,7 +870,7 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
private void installApk(File file) {
try {
installer.installPackage(file);
installer.installPackage(file, app.id);
} catch (AndroidNotCompatibleException e) {
Log.e(TAG, "Android not compatible with this Installer!", e);
}

View File

@ -27,6 +27,7 @@ import android.util.Log;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.privileged.install.InstallPrivilegedDialogActivity;
import java.io.File;
import java.util.List;
@ -146,13 +147,30 @@ abstract public class Installer {
return null;
}
public void installPackage(File apkFile) throws AndroidNotCompatibleException {
public void installPackage(File apkFile, String packageName) throws AndroidNotCompatibleException {
// check if file exists...
if (!apkFile.exists()) {
Log.e(TAG, "Couldn't find file " + apkFile + " to install.");
return;
}
// special case: Install F-Droid Privileged
if (packageName.equals(PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME)) {
Activity activity;
try {
activity = (Activity) mContext;
} catch (ClassCastException e) {
Log.d(TAG, "F-Droid Privileged can only be updated using an activity!");
return;
}
Intent installIntent = new Intent(activity, InstallPrivilegedDialogActivity.class);
installIntent.setAction(InstallPrivilegedDialogActivity.ACTION_INSTALL);
installIntent.putExtra(InstallPrivilegedDialogActivity.EXTRA_INSTALL_APK, apkFile.getAbsolutePath());
activity.startActivity(installIntent);
return;
}
installPackageInternal(apkFile);
}

View File

@ -74,8 +74,8 @@ public class PrivilegedInstaller extends Installer {
private static final String TAG = "PrivilegedInstaller";
private static final String PRIVILEGED_INTENT = "org.fdroid.fdroid.privileged.IPrivilegedService";
private static final String PRIVILEGED_PACKAGE_NAME = "org.fdroid.fdroid.privileged";
private static final String PRIVILEGED_SERVICE_INTENT = "org.fdroid.fdroid.privileged.IPrivilegedService";
public static final String PRIVILEGED_PACKAGE_NAME = "org.fdroid.fdroid.privileged";
private Activity mActivity;
@ -119,7 +119,7 @@ public class PrivilegedInstaller extends Installer {
public void onServiceDisconnected(ComponentName name) {
}
};
Intent serviceIntent = new Intent(PRIVILEGED_INTENT);
Intent serviceIntent = new Intent(PRIVILEGED_SERVICE_INTENT);
serviceIntent.setPackage(PRIVILEGED_PACKAGE_NAME);
context.getApplicationContext().bindService(serviceIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);
@ -210,7 +210,7 @@ public class PrivilegedInstaller extends Installer {
}
};
Intent serviceIntent = new Intent(PRIVILEGED_INTENT);
Intent serviceIntent = new Intent(PRIVILEGED_SERVICE_INTENT);
serviceIntent.setPackage(PRIVILEGED_PACKAGE_NAME);
mContext.getApplicationContext().bindService(serviceIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);
@ -316,7 +316,7 @@ public class PrivilegedInstaller extends Installer {
}
};
Intent serviceIntent = new Intent(PRIVILEGED_INTENT);
Intent serviceIntent = new Intent(PRIVILEGED_SERVICE_INTENT);
serviceIntent.setPackage(PRIVILEGED_PACKAGE_NAME);
mContext.getApplicationContext().bindService(serviceIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);

View File

@ -24,6 +24,7 @@ import android.os.Build;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.installer.PrivilegedInstaller;
import java.util.ArrayList;
import java.util.List;
@ -39,7 +40,7 @@ abstract class InstallPrivileged {
protected final Context context;
private static final String PACKAGE_NAME = "org.fdroid.fdroid.privileged";
private static final String APK_FILE_NAME = "FDroidPrivileged.apk";
public InstallPrivileged(final Context context) {
this.context = context;
@ -67,10 +68,10 @@ abstract class InstallPrivileged {
final void runUninstall() {
final String[] commands = {
"am force-stop " + PACKAGE_NAME,
"pm clear " + PACKAGE_NAME,
"am force-stop " + PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME,
"pm clear " + PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME,
"mount -o rw,remount /system",
"pm uninstall " + PACKAGE_NAME,
"pm uninstall " + PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME,
"rm -f " + getInstallPath(),
"sleep 5",
"mount -o ro,remount /system"
@ -84,19 +85,19 @@ abstract class InstallPrivileged {
}
protected String getInstallPath() {
return getSystemFolder() + "FDroidPrivileged.apk";
return getSystemFolder() + APK_FILE_NAME;
}
private List<String> getInstallCommands(String apkPath) {
final List<String> commands = new ArrayList<>();
commands.add("mount -o rw,remount /system");
commands.addAll(getCopyToSystemCommands(apkPath));
commands.add("pm uninstall " + PACKAGE_NAME);
commands.add("pm uninstall " + PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME);
commands.add("mv " + getInstallPath() + ".tmp " + getInstallPath());
commands.add("pm install -r " + getInstallPath());
commands.add("sleep 5"); // wait until the app is really installed
commands.add("mount -o ro,remount /system");
commands.add("am force-stop " + PACKAGE_NAME);
commands.add("am force-stop " + PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME);
commands.addAll(getPostInstallCommands());
return commands;
}

View File

@ -36,12 +36,15 @@ import android.text.Html;
import android.util.Log;
import android.view.ContextThemeWrapper;
import org.fdroid.fdroid.AppDetails;
import org.fdroid.fdroid.FDroid;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.installer.PrivilegedInstaller;
import java.io.File;
import eu.chainfire.libsuperuser.Shell;
/**
@ -52,11 +55,14 @@ public class InstallPrivilegedDialogActivity extends FragmentActivity {
private static final String TAG = "InstallIntoSystem";
public static final String ACTION_INSTALL = "install";
public static final String EXTRA_INSTALL_APK = "apk_file";
public static final String ACTION_UNINSTALL = "uninstall";
public static final String ACTION_POST_INSTALL = "post_install";
public static final String ACTION_FIRST_TIME = "first_time";
String action;
String apkFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -70,6 +76,8 @@ public class InstallPrivilegedDialogActivity extends FragmentActivity {
return;
}
apkFile = getIntent().getStringExtra(EXTRA_INSTALL_APK);
action = getIntent().getAction();
if (ACTION_UNINSTALL.equals(action)) {
uninstall();
@ -166,7 +174,11 @@ public class InstallPrivilegedDialogActivity extends FragmentActivity {
.setPositiveButton(R.string.system_permission_install_via_root, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
installTask.execute();
// Open details of F-Droid Privileged
Intent intent = new Intent(InstallPrivilegedDialogActivity.this, AppDetails.class);
intent.putExtra(AppDetails.EXTRA_APPID,
PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME);
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@ -268,7 +280,7 @@ public class InstallPrivilegedDialogActivity extends FragmentActivity {
@Override
protected Void doInBackground(Void... voids) {
InstallPrivileged.create(getApplicationContext()).runInstall("test"); // TODO
InstallPrivileged.create(getApplicationContext()).runInstall(apkFile);
return null;
}
};
@ -304,9 +316,9 @@ public class InstallPrivilegedDialogActivity extends FragmentActivity {
// hack to get holo design (which is not automatically applied due to activity's Theme.NoDisplay
ContextThemeWrapper theme = new ContextThemeWrapper(this, FDroidApp.getCurThemeResId());
final boolean systemApp = PrivilegedInstaller.isAvailable(this);
final boolean isAvailable = PrivilegedInstaller.isAvailable(this);
if (systemApp) {
if (isAvailable) {
AlertDialog.Builder builder = new AlertDialog.Builder(theme)
.setTitle(R.string.system_uninstall)
.setMessage(R.string.system_uninstall_message)

View File

@ -14,6 +14,7 @@ import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
import org.fdroid.fdroid.AppDetails;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.PreferencesActivity;
@ -221,9 +222,11 @@ public class PreferencesFragment extends PreferenceFragment
alertBuilder.setPositiveButton(R.string.system_permission_install_via_root, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent installIntent = new Intent(getActivity(), InstallPrivilegedDialogActivity.class);
installIntent.setAction(InstallPrivilegedDialogActivity.ACTION_INSTALL);
startActivity(installIntent);
// Open details of F-Droid Privileged
Intent intent = new Intent(getActivity(), AppDetails.class);
intent.putExtra(AppDetails.EXTRA_APPID,
PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME);
startActivity(intent);
}
});
alertBuilder.setNegativeButton(R.string.cancel, null);