Skip to content

Commit

Permalink
[Refactor] Add JsonDeserializer for QueueItem classes and any depende…
Browse files Browse the repository at this point in the history
…ncies

Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Jan 27, 2025
1 parent b1312ef commit a96a7a7
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONException;

import io.github.muntashirakon.AppManager.history.IJsonSerializer;
import io.github.muntashirakon.AppManager.history.JsonDeserializer;
import io.github.muntashirakon.AppManager.utils.JSONUtils;

public abstract class ApkSource implements Parcelable, IJsonSerializer {
@NonNull
Expand All @@ -35,4 +39,15 @@ public static ApkSource getApkSource(@NonNull ApplicationInfo applicationInfo) {
@AnyThread
@NonNull
public abstract ApkSource toCachedSource();

public static final JsonDeserializer.Creator<ApkSource> DESERIALIZER = jsonObject -> {
String tag = JSONUtils.getString(jsonObject, "tag");
if (ApplicationInfoApkSource.TAG.equals(tag)) {
return ApplicationInfoApkSource.DESERIALIZER.deserialize(jsonObject);
} else if (CachedApkSource.TAG.equals(tag)) {
return CachedApkSource.DESERIALIZER.deserialize(jsonObject);
} else if (UriApkSource.TAG.equals(tag)) {
return UriApkSource.DESERIALIZER.deserialize(jsonObject);
} else throw new JSONException("Invalid tag: " + tag);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package io.github.muntashirakon.AppManager.apk;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Parcel;

Expand All @@ -15,7 +17,12 @@
import java.io.File;
import java.util.Objects;

import io.github.muntashirakon.AppManager.history.JsonDeserializer;
import io.github.muntashirakon.AppManager.utils.ContextUtils;

public class ApplicationInfoApkSource extends ApkSource {
public static final String TAG = ApplicationInfoApkSource.class.getSimpleName();

@NonNull
private final ApplicationInfo mApplicationInfo;

Expand Down Expand Up @@ -61,15 +68,27 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mApkFileKey);
}

protected ApplicationInfoApkSource(@NonNull JSONObject jsonObject) throws JSONException {
PackageManager pm = ContextUtils.getContext().getPackageManager();
String file = jsonObject.getString("file");
PackageInfo packageInfo = Objects.requireNonNull(pm.getPackageArchiveInfo(file, 0));
mApplicationInfo = Objects.requireNonNull(packageInfo.applicationInfo);
mApplicationInfo.publicSourceDir = mApplicationInfo.sourceDir = file;
mApkFileKey = jsonObject.getInt("apk_file_key");
}

@NonNull
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("file", mApplicationInfo.publicSourceDir);
jsonObject.put("apk_file_key", mApkFileKey);
return jsonObject;
}

public static final JsonDeserializer.Creator<ApplicationInfoApkSource> DESERIALIZER = ApplicationInfoApkSource::new;

public static final Creator<ApplicationInfoApkSource> CREATOR = new Creator<ApplicationInfoApkSource>() {
@Override
public ApplicationInfoApkSource createFromParcel(Parcel source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import java.util.Objects;

import io.github.muntashirakon.AppManager.fm.FmProvider;
import io.github.muntashirakon.AppManager.history.JsonDeserializer;
import io.github.muntashirakon.AppManager.self.filecache.FileCache;
import io.github.muntashirakon.AppManager.utils.FileUtils;
import io.github.muntashirakon.io.Paths;

public class CachedApkSource extends ApkSource {
public static final String TAG = CachedApkSource.class.getSimpleName();

@NonNull
private final Uri mUri;
private final String mMimeType;
Expand Down Expand Up @@ -101,17 +104,27 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeString(file);
}

protected CachedApkSource(@NonNull JSONObject jsonObject) throws JSONException {
mUri = Uri.parse(jsonObject.getString("uri"));
mMimeType = jsonObject.getString("mime_type");
mApkFileKey = jsonObject.getInt("apk_file_key");
mCachedFile = new File(jsonObject.getString("cached_file"));
}

@NonNull
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("uri", mUri.toString());
jsonObject.put("mime_type", mMimeType);
jsonObject.put("apk_file_key", mApkFileKey);
jsonObject.put("cached_file", mCachedFile);
jsonObject.put("cached_file", mCachedFile.getAbsolutePath());
return jsonObject;
}

public static final JsonDeserializer.Creator<CachedApkSource> DESERIALIZER = CachedApkSource::new;

public static final Creator<CachedApkSource> CREATOR = new Creator<CachedApkSource>() {
@Override
public CachedApkSource createFromParcel(Parcel source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import java.util.Objects;

import io.github.muntashirakon.AppManager.history.JsonDeserializer;

public class UriApkSource extends ApkSource {
public static final String TAG = UriApkSource.class.getSimpleName();

@NonNull
private final Uri mUri;
@Nullable
Expand Down Expand Up @@ -63,16 +67,25 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mApkFileKey);
}

protected UriApkSource(@NonNull JSONObject jsonObject) throws JSONException {
mUri = Uri.parse(jsonObject.getString("uri"));
mMimeType = jsonObject.getString("mime_type");
mApkFileKey = jsonObject.getInt("apk_file_key");
}

@NonNull
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("uri", mUri.toString());
jsonObject.put("mime_type", mMimeType);
jsonObject.put("apk_file_key", mApkFileKey);
return jsonObject;
}

public static final JsonDeserializer.Creator<UriApkSource> DESERIALIZER = UriApkSource::new;

public static final Creator<UriApkSource> CREATOR = new Creator<UriApkSource>() {
@Override
public UriApkSource createFromParcel(Parcel source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import io.github.muntashirakon.AppManager.apk.ApkSource;
import io.github.muntashirakon.AppManager.history.IJsonSerializer;
import io.github.muntashirakon.AppManager.history.JsonDeserializer;
import io.github.muntashirakon.AppManager.intercept.IntentCompat;
import io.github.muntashirakon.AppManager.utils.JSONUtils;

Expand Down Expand Up @@ -174,6 +175,20 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeStringList(mSelectedSplits);
}

protected ApkQueueItem(@NonNull JSONObject jsonObject) throws JSONException {
mPackageName = JSONUtils.optString(jsonObject, "package_name", null);
mAppLabel = JSONUtils.optString(jsonObject, "app_label", null);
mInstallExisting = jsonObject.optBoolean("install_existing", false);
mOriginatingPackage = JSONUtils.optString(jsonObject, "originating_package", null);
String originatingUri = JSONUtils.optString(jsonObject, "originating_uri", null);
mOriginatingUri = originatingUri != null ? Uri.parse(originatingUri) : null;
JSONObject apkSource = jsonObject.optJSONObject("apk_source");
mApkSource = apkSource != null ? ApkSource.DESERIALIZER.deserialize(apkSource) : null;
JSONObject installerOptions = jsonObject.optJSONObject("installer_options");
mInstallerOptions = installerOptions != null ? InstallerOptions.DESERIALIZER.deserialize(installerOptions) : null;
mSelectedSplits = JSONUtils.getArray(jsonObject.optJSONArray("selected_splits"));
}

@NonNull
@Override
public JSONObject serializeToJson() throws JSONException {
Expand All @@ -189,6 +204,8 @@ public JSONObject serializeToJson() throws JSONException {
return jsonObject;
}

public static final JsonDeserializer.Creator<ApkQueueItem> DESERIALIZER = ApkQueueItem::new;

public static final Creator<ApkQueueItem> CREATOR = new Creator<ApkQueueItem>() {
@Override
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import io.github.muntashirakon.AppManager.BuildConfig;
import io.github.muntashirakon.AppManager.history.IJsonSerializer;
import io.github.muntashirakon.AppManager.history.JsonDeserializer;
import io.github.muntashirakon.AppManager.settings.Prefs;
import io.github.muntashirakon.AppManager.utils.JSONUtils;

public class InstallerOptions implements Parcelable, IJsonSerializer {
@NonNull
Expand Down Expand Up @@ -107,6 +109,21 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeByte((byte) (mBlockTrackers ? 1 : 0));
}

protected InstallerOptions(@NonNull JSONObject jsonObject) throws JSONException {
mUserId = jsonObject.getInt("user_id");
mInstallLocation = jsonObject.getInt("install_location");
mInstallerName = JSONUtils.optString(jsonObject, "installer_name", null);
mOriginatingPackage = JSONUtils.optString(jsonObject, "originating_package");
String originatingUri = JSONUtils.optString(jsonObject, "originating_uri", null);
mOriginatingUri = originatingUri != null ? Uri.parse(originatingUri) : null;
mPackageSource = jsonObject.getInt("package_source");
mInstallScenario = jsonObject.getInt("install_scenario");
mRequestUpdateOwnership = jsonObject.getBoolean("request_update_ownership");
mSignApkFiles = jsonObject.getBoolean("sign_apk_files");
mForceDexOpt = jsonObject.getBoolean("force_dex_opt");
mBlockTrackers = jsonObject.getBoolean("block_trackers");
}

@NonNull
@Override
public JSONObject serializeToJson() throws JSONException {
Expand All @@ -125,6 +142,8 @@ public JSONObject serializeToJson() throws JSONException {
return jsonObject;
}

public static final JsonDeserializer.Creator<InstallerOptions> DESERIALIZER = InstallerOptions::new;

@Override
public int describeContents() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.github.muntashirakon.AppManager.batchops.struct.BatchPermissionOptions;
import io.github.muntashirakon.AppManager.batchops.struct.IBatchOpOptions;
import io.github.muntashirakon.AppManager.history.IJsonSerializer;
import io.github.muntashirakon.AppManager.history.JsonDeserializer;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.JSONUtils;
import io.github.muntashirakon.util.ParcelUtils;
Expand Down Expand Up @@ -138,6 +139,15 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeParcelable(mOptions, flags);
}

protected BatchQueueItem(@NonNull JSONObject jsonObject) throws JSONException {
mTitleRes = jsonObject.getInt("title_res");
mOp = jsonObject.getInt("op");
mPackages = JSONUtils.getArray(jsonObject.getJSONArray("packages"));
mUsers = JSONUtils.getArray(jsonObject.getJSONArray("users"));
JSONObject options = jsonObject.optJSONObject("options");
mOptions = options != null ? IBatchOpOptions.DESERIALIZER.deserialize(options) : null;
}

@NonNull
@Override
public JSONObject serializeToJson() throws JSONException {
Expand All @@ -158,6 +168,8 @@ protected BatchQueueItem(@NonNull Parcel in) {
mOptions = readOptionsFromParcel(in, mOp);
}

public static final JsonDeserializer.Creator<BatchQueueItem> DESERIALIZER = BatchQueueItem::new;

public static final Creator<BatchQueueItem> CREATOR = new Creator<BatchQueueItem>() {
@NonNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import io.github.muntashirakon.AppManager.utils.JSONUtils;

public class BatchAppOpsOptions implements IBatchOpOptions {
public static final String TAG = BatchAppOpsOptions.class.getSimpleName();

@NonNull
private int[] mAppOps;
private int mMode;
Expand Down Expand Up @@ -66,6 +68,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("app_ops", JSONUtils.getJSONArray(mAppOps));
jsonObject.put("mode", mMode);
return jsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.github.muntashirakon.AppManager.backup.convert.ImportType;

public class BatchBackupImportOptions implements IBatchOpOptions {
public static final String TAG = BatchBackupImportOptions.class.getSimpleName();

@ImportType
private int mImportType;
@NonNull
Expand Down Expand Up @@ -78,6 +80,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("import_type", mImportType);
jsonObject.put("directory", mDirectory.toString());
jsonObject.put("remove_imported_directory", mRemoveImportedDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import io.github.muntashirakon.AppManager.utils.JSONUtils;

public class BatchBackupOptions implements IBatchOpOptions {
public static final String TAG = BatchBackupOptions.class.getSimpleName();

@BackupFlags.BackupFlag
private int mFlags;
@Nullable
Expand Down Expand Up @@ -67,6 +69,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("flags", mFlags);
jsonObject.put("backup_names", JSONUtils.getJSONArray(mBackupNames));
return jsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import io.github.muntashirakon.AppManager.utils.JSONUtils;

public class BatchComponentOptions implements IBatchOpOptions {
public static final String TAG = BatchComponentOptions.class.getSimpleName();

private String[] mSignatures;

public BatchComponentOptions(@NonNull String[] signatures) {
Expand All @@ -38,6 +40,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("signatures", JSONUtils.getJSONArray(mSignatures));
return jsonObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.github.muntashirakon.AppManager.apk.dexopt.DexOptOptions;

public class BatchDexOptOptions implements IBatchOpOptions {
public static final String TAG = BatchDexOptOptions.class.getSimpleName();
private DexOptOptions mDexOptOptions;

public BatchDexOptOptions(@NonNull DexOptOptions dexOptOptions) {
Expand Down Expand Up @@ -56,6 +57,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("dex_opt_options", mDexOptOptions.serializeToJson());
return jsonObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.github.muntashirakon.AppManager.compat.NetworkPolicyManagerCompat.NetPolicy;

public class BatchNetPolicyOptions implements IBatchOpOptions {
public static final String TAG = BatchNetPolicyOptions.class.getSimpleName();
@NetPolicy
private int mPolicies;

Expand Down Expand Up @@ -56,6 +57,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("policies", mPolicies);
return jsonObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Objects;

public class BatchPermissionOptions implements IBatchOpOptions {
public static final String TAG = BatchPermissionOptions.class.getSimpleName();
private String[] mPermissions;

public BatchPermissionOptions(@NonNull String[] permissions) {
Expand All @@ -36,6 +37,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
@Override
public JSONObject serializeToJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("tag", TAG);
jsonObject.put("permissions", mPermissions);
return jsonObject;
}
Expand Down
Loading

0 comments on commit a96a7a7

Please sign in to comment.