Skip to content

Commit

Permalink
Merge pull request #130 from madhawap/master
Browse files Browse the repository at this point in the history
Verifying package name before uninstalling an application
  • Loading branch information
charithag authored Dec 22, 2017
2 parents c28a29a + a4a35ca commit adffe9c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ private void provisionManagedProfile() {
activity.getApplicationContext().getPackageName());
// Once the provisioning is done, user is prompted to uninstall the agent in personal profile.
ApplicationManager applicationManager = new ApplicationManager(this.getApplicationContext());
applicationManager.uninstallApplication(Constants.AGENT_PACKAGE, null);
try {
applicationManager.uninstallApplication(Constants.AGENT_PACKAGE, null);
} catch (AndroidAgentException e) {
Log.e(TAG,"App uninstallation failed");
}

if (intent.resolveActivity(activity.getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class ApplicationManager {
private static final String APP_STATE_DOWNLOAD_FAILED = "DOWNLOAD_FAILED";
private static final String APP_STATE_INSTALL_FAILED = "INSTALL_FAILED";
private static final String APP_STATE_INSTALLED = "INSTALLED";
private static final String APP_STATE_UNINSTALLED = "UNINSTALLED";
private static final String APP_STATE_UNINSTALLED_FAILED = "UNINSTALL_FAILED";
private static final String TAG = ApplicationManager.class.getName();
private static final String APP_INSTALLATION_ATTEMPT = "APP_INSTALLATION_ATTEMPT";
private static volatile boolean downloadOngoing = false;
Expand Down Expand Up @@ -513,11 +515,19 @@ public void setupAppDownload(String url, int operationId, String operationCode)
*
* @param packageName - Application package name should be passed in as a String.
*/
public void uninstallApplication(String packageName, String schedule) {
public void uninstallApplication(String packageName, String schedule) throws AndroidAgentException {
if (packageName != null &&
!packageName.contains(resources.getString(R.string.application_package_prefix))) {
packageName = resources.getString(R.string.application_package_prefix) + packageName;
}

if(!this.isPackageInstalled(packageName)){
String message = "Package is not installed in the device or invalid package name";
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_status), APP_STATE_UNINSTALLED_FAILED);
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_failed_message), message);
throw new AndroidAgentException("Package is not installed in the device");
}

if (schedule != null && !schedule.trim().isEmpty() && !schedule.equals("undefined")) {
try {
AlarmUtils.setOneTimeAlarm(context, schedule, Constants.Operation.UNINSTALL_APPLICATION, null, null, packageName);
Expand All @@ -527,8 +537,12 @@ public void uninstallApplication(String packageName, String schedule) {
return; //Will call uninstallApplication method again upon alarm.
}
if (Constants.SYSTEM_APP_ENABLED) {
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_status), APP_STATE_UNINSTALLED);
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_failed_message), null);
CommonUtils.callSystemApp(context, Constants.Operation.SILENT_UNINSTALL_APPLICATION, "", packageName);
} else {
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_status), APP_STATE_UNINSTALLED);
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_failed_message), null);
Uri packageURI = Uri.parse(packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
uninstallIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Expand Down Expand Up @@ -599,6 +613,14 @@ public Operation getApplicationInstallationStatus(Operation operation, String st
operation.setStatus(context.getResources().getString(R.string.operation_value_completed));
operation.setOperationResponse("Application installation completed");
break;
case APP_STATE_UNINSTALLED_FAILED:
operation.setStatus(context.getResources().getString(R.string.operation_value_error));
operation.setOperationResponse(message);
break;
case APP_STATE_UNINSTALLED:
operation.setStatus(context.getResources().getString(R.string.operation_value_completed));
operation.setOperationResponse("Application uninstallation completed");
break;
default:

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public void onReceive(Context context, Intent intent) {
applicationManager.installApp(appUrl, null, operation);
} else if(operationCode != null && operationCode.trim().equals(Constants.Operation.UNINSTALL_APPLICATION)) {
String packageUri = intent.getStringExtra(context.getResources().getString(R.string.app_uri));
applicationManager.uninstallApplication(packageUri, null);
try {
applicationManager.uninstallApplication(packageUri, null);
} catch (AndroidAgentException e) {
Log.e(TAG,"App uninstallation failed");
}
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public void doTask(String operation) {
break;
case Constants.Operation.UNINSTALL_APPLICATION:
if (appUri != null) {
applicationManager.uninstallApplication(appUri, null);
try {
applicationManager.uninstallApplication(appUri, null);
} catch (AndroidAgentException e) {
Log.e(TAG,"App uninstallation failed");
}
} else {
Toast.makeText(context, context.getResources().getString(R.string.toast_app_removal_failed),
Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,36 @@ public void getMessages() throws AndroidAgentException {
R.string.firmware_upgrade_response_message), null);
}


int applicationUninstallOperationId = Preference.getInt(context, context.getResources().getString(
R.string.app_uninstall_id));
String applicationUninstallOperationCode = Preference.getString(context, context.getResources().getString(
R.string.app_uninstall_code));
String applicationUninstallOperationStatus = Preference.getString(context, context.getResources().getString(
R.string.app_uninstall_status));
String applicationUninstallOperationMessage = Preference.getString(context, context.getResources().getString(
R.string.app_uninstall_failed_message));

if (applicationUninstallOperationStatus != null && applicationUninstallOperationId != 0 && applicationUninstallOperationCode != null) {
Operation applicationOperation = new Operation();
ApplicationManager appMgt = new ApplicationManager(context);
applicationOperation.setId(applicationUninstallOperationId);
applicationOperation.setCode(applicationUninstallOperationCode);
applicationOperation = appMgt.getApplicationInstallationStatus(
applicationOperation, applicationUninstallOperationStatus, applicationUninstallOperationMessage);
if (replyPayload == null) {
replyPayload = new ArrayList<>();
}
replyPayload.add(applicationOperation);

Preference.putString(context, context.getResources().getString(
R.string.app_install_status), null);
Preference.putString(context, context.getResources().getString(
R.string.app_install_failed_message), null);
}



int applicationOperationId = Preference.getInt(context, context.getResources().getString(
R.string.app_install_id));
String applicationOperationCode = Preference.getString(context, context.getResources().getString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,11 @@ public void uninstallApplication(Operation operation) throws AndroidAgentExcepti
if (appData.has(getContextResources().getString(R.string.app_schedule))) {
schedule = appData.getString(getContextResources().getString(R.string.app_schedule));
}
int operationId = operation.getId();
String operationCode = operation.getCode();
Preference.putInt(context, context.getResources().getString(R.string.app_uninstall_id), operationId);
Preference.putString(context, context.getResources().getString(R.string.app_uninstall_code), operationCode);
getAppList().uninstallApplication(packageName, schedule);
operation.setStatus(getContextResources().getString(R.string.operation_value_completed));
getResultBuilder().build(operation);
}

if (Constants.DEBUG_MODE_ENABLED) {
Expand Down
4 changes: 4 additions & 0 deletions client/client/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,13 @@
<string name="is_automatic_firmware_upgrade">isAutomaticFirmwareUpgrade</string>
<string name="firmware_upgrade_automatic_retry">autoRetry</string>
<string name="app_install_failed_message">appInstallFailedMessage</string>
<string name="app_uninstall_failed_message">appUnInstallFailedMessage</string>
<string name="app_uninstall_id">appUninstallOperationId</string>
<string name="app_install_id">appInstallOperationId</string>
<string name="app_install_code">appInstallOperationCode</string>
<string name="app_uninstall_code">appUninstallOperationCode</string>
<string name="app_install_status">appInstallStatus</string>
<string name="app_uninstall_status">appUnInstallStatus</string>
<string name="operation_receivedTimeStamp">receivedTimeStamp</string>
<string name="operation_createdTimeStamp">createdTimeStamp</string>
<string name="operation_payLoad">payLoad</string>
Expand Down

0 comments on commit adffe9c

Please sign in to comment.