cmsdk: Refactor Parts to use RemotePreference

* Delete ALL the codes!

Change-Id: I839ba927f03363601452bdc3d894e09549366302
This commit is contained in:
Steve Kondik
2016-10-15 00:58:25 -07:00
parent 3805419c29
commit 26977b4a75
3 changed files with 34 additions and 146 deletions

View File

@@ -16,15 +16,16 @@
package org.cyanogenmod.internal.cmparts;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import cyanogenmod.preference.SelfRemovingPreference;
import cyanogenmod.preference.RemotePreference;
/**
* A link to a remote preference screen which can be used with a minimum amount
* of information. Supports summary updates asynchronously.
*/
public class CMPartsPreference extends SelfRemovingPreference implements PartInfo.RemotePart {
public class CMPartsPreference extends RemotePreference {
private static final String TAG = "CMPartsPreference";
@@ -32,38 +33,51 @@ public class CMPartsPreference extends SelfRemovingPreference implements PartInf
private final Context mContext;
public CMPartsPreference(Context context, AttributeSet attrs) {
super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
public CMPartsPreference(Context context, AttributeSet attrs,
int defStyle, int defStyleRes) {
super(context, attrs, defStyle, defStyleRes);
mContext = context;
mPart = PartsList.get(context).getPartInfo(getKey());
if (mPart == null) {
throw new RuntimeException("Part not found: " + getKey());
}
if (!mPart.isAvailable()) {
setAvailable(false);
}
updatePreference();
setIntent(mPart.getIntentForActivity());
}
onRefresh(context, mPart);
public CMPartsPreference(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs, defStyle, 0);
}
public CMPartsPreference(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
}
@Override
public void onAttached() {
super.onAttached();
mPart.registerRemote(mContext, this);
public void onRemoteUpdated(Bundle bundle) {
if (bundle.containsKey(PartsList.EXTRA_PART)) {
PartInfo update = bundle.getParcelable(PartsList.EXTRA_PART);
if (update != null) {
mPart.updateFrom(update);
updatePreference();
}
}
}
@Override
public void onDetached() {
super.onDetached();
mPart.unregisterRemote(mContext, this);
protected String getRemoteKey(Bundle metaData) {
// remote key is the same as ours
return getKey();
}
@Override
public void onRefresh(Context context, PartInfo info) {
setTitle(mPart.getTitle());
setSummary((CharSequence) mPart.getSummary());
private void updatePreference() {
if (isAvailable() != mPart.isAvailable()) {
setAvailable(mPart.isAvailable());
}
if (isAvailable()) {
setTitle(mPart.getTitle());
setSummary(mPart.getSummary());
}
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.cyanogenmod.internal.cmparts;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
@@ -185,18 +184,6 @@ public class PartInfo implements Parcelable {
return i;
}
public interface RemotePart {
public void onRefresh(Context context, PartInfo info);
}
public void registerRemote(Context context, final RemotePart remote) {
PartsList.get(context).registerRemotePart(mName, remote);
}
public void unregisterRemote(Context context, final RemotePart remote) {
PartsList.get(context).unregisterRemotePart(mName, remote);
}
public static final Parcelable.Creator<PartInfo> CREATOR = new Parcelable.Creator<PartInfo>() {
@Override
public PartInfo createFromParcel(Parcel in) {

View File

@@ -15,20 +15,13 @@
*/
package org.cyanogenmod.internal.cmparts;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
@@ -43,8 +36,6 @@ import java.io.IOException;
import java.util.Map;
import java.util.Set;
import cyanogenmod.platform.Manifest;
import static com.android.internal.R.styleable.Preference;
import static com.android.internal.R.styleable.Preference_fragment;
import static com.android.internal.R.styleable.Preference_icon;
@@ -60,27 +51,17 @@ public class PartsList {
private static final boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
public static final String ACTION_PART_CHANGED = "org.cyanogenmod.cmparts.PART_CHANGED";
public static final String ACTION_REFRESH_PART = "org.cyanogenmod.cmparts.REFRESH_PART";
public static final String EXTRA_PART = "part";
public static final String EXTRA_PART_KEY = "key";
public static final String EXTRA_PART_SUMMARY = "summary";
public static final String EXTRA_PART = ":cm:part";
public static final String CMPARTS_PACKAGE = "org.cyanogenmod.cmparts";
public static final ComponentName CMPARTS_ACTIVITY = new ComponentName(
CMPARTS_PACKAGE, CMPARTS_PACKAGE + ".PartsActivity");
public static final ComponentName CMPARTS_REFRESHER = new ComponentName(
CMPARTS_PACKAGE, CMPARTS_PACKAGE + ".RefreshReceiver");
public static final String PARTS_ACTION_PREFIX = CMPARTS_PACKAGE + ".parts";
private final Map<String, PartInfo> mParts = new ArrayMap<>();
private final Map<String, Set<PartInfo.RemotePart>> mRemotes = new ArrayMap<>();
private final Context mContext;
private static PartsList sInstance;
@@ -228,98 +209,4 @@ public class PartsList {
if (parser != null) parser.close();
}
}
public void registerRemotePart(final String key, final PartInfo.RemotePart remote) {
synchronized (mParts) {
if (DEBUG) {
Log.v(TAG, "registerRemotePart part=" + key + " remote=" + remote.toString());
}
if (mRemotes.size() == 0) {
final IntentFilter filter = new IntentFilter(ACTION_PART_CHANGED);
mContext.registerReceiver(mPartChangedReceiver, filter,
Manifest.permission.MANAGE_REMOTE_PREFERENCES, null);
}
Set<PartInfo.RemotePart> remotes = mRemotes.get(key);
if (remotes == null) {
remotes = new ArraySet<PartInfo.RemotePart>();
mRemotes.put(key, remotes);
}
remotes.add(remote);
final Intent i = new Intent(ACTION_REFRESH_PART);
i.setComponent(PartsList.CMPARTS_REFRESHER);
i.putExtra(EXTRA_PART_KEY, key);
// Send an ordered broadcast to request a refresh and receive the reply
// on the BroadcastReceiver.
mContext.sendOrderedBroadcastAsUser(i, UserHandle.CURRENT,
Manifest.permission.MANAGE_REMOTE_PREFERENCES,
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
synchronized (mParts) {
refreshPartFromBundleLocked(getResultExtras(true));
}
}
}, null, Activity.RESULT_OK, null, null);
}
}
private void refreshPartFromBundleLocked(Bundle result) {
PartInfo info = mParts.get(result.getString(EXTRA_PART_KEY));
if (info != null) {
PartInfo updatedPart = (PartInfo) result.getParcelable(EXTRA_PART);
if (updatedPart != null) {
if (info.updateFrom(updatedPart)) {
Set<PartInfo.RemotePart> remotes = mRemotes.get(info.getName());
if (remotes != null && remotes.size() > 0) {
for (PartInfo.RemotePart remote : remotes) {
if (DEBUG) {
Log.d(TAG, "refresh remote=" + remote.toString() +
" info=" + info.toString());
}
remote.onRefresh(mContext, info);
}
}
}
}
}
}
public void unregisterRemotePart(String key, final PartInfo.RemotePart remote) {
synchronized (mParts) {
if (DEBUG) {
Log.d(TAG, "unregisterRemotePart: " + key + " remote=" + remote.toString());
}
Set<PartInfo.RemotePart> remotes = mRemotes.get(key);
if (remotes != null) {
remotes.remove(remote);
if (remotes.size() == 0) {
mRemotes.remove(key);
if (mRemotes.size() == 0) {
mContext.unregisterReceiver(mPartChangedReceiver);
}
}
}
}
}
/**
* Receiver for asynchronous updates
*/
private final BroadcastReceiver mPartChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
synchronized (mParts) {
if (DEBUG) {
Log.d(TAG, "PART_CHANGED: " + intent.toString() +
" bundle: " + intent.getExtras().toString());
}
refreshPartFromBundleLocked(intent.getExtras());
}
}
};
}