Skip to content

Commit

Permalink
Merge pull request #196 from charithag/master
Browse files Browse the repository at this point in the history
Fix issues in app install & uninstall
  • Loading branch information
charithag authored Oct 24, 2018
2 parents f043e9e + d15799d commit 7c87740
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 61 deletions.
2 changes: 1 addition & 1 deletion client/client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@

<!-- android 7 requirement -->
<provider
android:name="android.support.v4.content.FileProvider"
android:name=".AgentFileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ requestCode, new Intent(getApplicationContext(), EnrollmentService.class),
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

public void onCreate(){
public void onCreate() {
super.onCreate();

if (Constants.LogPublisher.LOG_PUBLISHER_IN_USE.equals(Constants.LogPublisher.SPLUNK_PUBLISHER)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.iot.agent;

import android.support.v4.content.FileProvider;

public class AgentFileProvider extends FileProvider {
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,8 @@ public void onReceive(Context context, Intent intent) {
}
Preference.putString(context, context.getResources().getString(R.string.shared_pref_installed_file),
resources.getString(R.string.download_mgr_download_file_name));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkURI = FileProvider.getUriForFile(
context,
context.getApplicationContext()
.getPackageName() + ".provider", new File(downloadDirectoryPath + File.separator +
resources.getString(R.string.download_mgr_download_file_name)));
triggerInstallation(apkURI);

} else {
triggerInstallation(Uri.fromFile(new File(downloadDirectoryPath + File.separator +
resources.getString(R.string.download_mgr_download_file_name))));
}

triggerInstallation(Uri.fromFile(new File(downloadDirectoryPath + File.separator +
resources.getString(R.string.download_mgr_download_file_name))));
} else {
Preference.putString(context, context.getResources().getString(
R.string.app_install_status), Constants.AppState.DOWNLOAD_FAILED);
Expand Down Expand Up @@ -262,27 +250,6 @@ public List<String> getAppsOfUser() {
return packagesInstalledByUser;
}

/**
* Returns the app name for a particular package name.
*
* @param packageName - Package name which you need the app name.
* @return - Application name.
*/
public String getAppNameFromPackage(String packageName) {
String appName = null;
List<PackageInfo> packages = packageManager.
getInstalledPackages(SYSTEM_APPS_DISABLED_FLAG);
for (PackageInfo packageInfo : packages) {
if (packageName.equals(packageInfo.packageName)) {
appName = packageInfo.applicationInfo.
loadLabel(packageManager).toString();
break;
}
}

return appName;
}

public boolean isPackageInstalled(String packagename) {
try {
PackageInfo packageInfo = packageManager.getPackageInfo(packagename, 0);
Expand Down Expand Up @@ -316,20 +283,16 @@ private void triggerInstallation(Uri fileUri) {
private void startInstallerIntent(Uri fileUri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
policyManager.isDeviceOwnerApp(Constants.AGENT_PACKAGE)) {
Uri packageFileUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
packageFileUri = convertContentUriToFileUri(fileUri);
} else {
packageFileUri = fileUri;
}
installPackage(packageFileUri);
installPackage(fileUri);
} else {
boolean isUnknownSourcesDisallowed = Preference.getBoolean(context,
Constants.PreferenceFlag.DISALLOW_UNKNOWN_SOURCES);
CommonUtils.allowUnknownSourcesForProfile(context, !isUnknownSourcesDisallowed);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setDataAndType(fileUri, resources.getString(R.string.application_mgr_mime));
Uri apkURI = FileProvider.getUriForFile(context,
Constants.AGENT_PACKAGE + ".provider", new File(fileUri.getPath()));
intent.setDataAndType(apkURI, resources.getString(R.string.application_mgr_mime));
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
} else {
Expand All @@ -341,14 +304,6 @@ private void startInstallerIntent(Uri fileUri) {
}
}

private Uri convertContentUriToFileUri(Uri contentUri) {
String uriString = contentUri.toString();
uriString = "file://" + Environment.getExternalStorageDirectory() +
uriString.replace("content://" + Constants.AGENT_PACKAGE
+ ".provider/external_files", "");
return Uri.parse(uriString);
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private boolean installPackage(Uri fileUri) {

Expand Down Expand Up @@ -377,8 +332,6 @@ private boolean installPackage(Uri fileUri) {

session.commit(createIntentSender(context, sessionId, packageName));
return true;
} catch (IOException e) {
Log.e(TAG, "Error occurred while installing application '" + packageName + "'", e);
} catch (Exception e) {
Log.e(TAG, "Error occurred while installing application '" + packageName + "'", e);
}
Expand Down Expand Up @@ -618,11 +571,6 @@ public void uninstallApplication(String packageName, Operation operation, String
Calendar.getInstance().getTimeInMillis());
}

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 '" + packageName + "' is not installed in the device or invalid package name";
if (operation != null) {
Expand All @@ -639,7 +587,12 @@ public void uninstallApplication(String packageName, Operation operation, String
context.getResources().getString(R.string.app_uninstall_failed_message),
message);
}
throw new AndroidAgentException("Package '" + packageName + "' is not installed in the device");
throw new AndroidAgentException(message);
}

if (packageName != null &&
!packageName.contains(resources.getString(R.string.application_package_prefix))) {
packageName = resources.getString(R.string.application_package_prefix) + packageName;
}

if (Constants.SYSTEM_APP_ENABLED) {
Expand Down

0 comments on commit 7c87740

Please sign in to comment.