Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ee8f6e86 authored by Steve Kondik's avatar Steve Kondik
Browse files

cmparts: Add PartsRefresher for updating remote UI components

 * We need to keep the UI up to date with changes to the
   state of parts, for example to refresh the summary of a
   preference when it's changed by the user.
 * This works using broadcasts.

Change-Id: I07bf1358535db6ba40f8ee5a2826537f75e34cba
parent 3b9691d6
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -36,10 +36,11 @@
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_SEARCH_INDEXABLES" />

    <uses-permission android:name="cyanogenmod.permission.BIND_CORE_SERVICE" />
    <uses-permission android:name="cyanogenmod.permission.MANAGE_PARTS" />

    <protected-broadcast android:name="cyanogenmod.platform.app.profiles.PROFILES_STATE_CHANGED" />
    <protected-broadcast android:name="org.cyanogenmod.cmparts.PART_CHANGED" />
    <protected-broadcast android:name="org.cyanogenmod.cmparts.REFRESH_PART" />

    <application android:label="@string/cmparts_title"
            android:theme="@style/Theme.Settings"
@@ -61,6 +62,12 @@
            </intent-filter>
        </receiver>

        <receiver android:name=".RefreshReceiver" android:enabled="true">
            <intent-filter>
                <action android:name="org.cyanogenmod.cmparts.REFRESH_PART" />
            </intent-filter>
        </receiver>

        <provider android:name=".search.CMPartsSearchIndexablesProvider"
                  android:authorities="org.cyanogenmod.cmparts"
                  android:multiprocess="false"
+2 −2
Original line number Diff line number Diff line
@@ -93,10 +93,10 @@ public class PartsActivity extends SettingsDrawerActivity implements
        if (fragmentClass == null) {
            if (partExtra != null) {
                // Parts mode
                info = PartsList.getPartInfo(this, partExtra);
                info = PartsList.get(this).getPartInfo(partExtra);
            } else {
                // Alias mode
                info = PartsList.getPartInfoForClass(this,
                info = PartsList.get(this).getPartInfoForClass(
                        getIntent().getComponent().getClassName());
                mHomeAsUp = false;
            }
+220 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed 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.cyanogenmod.cmparts;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

import org.cyanogenmod.internal.cmparts.PartInfo;
import org.cyanogenmod.internal.cmparts.PartsList;
import org.cyanogenmod.platform.internal.Manifest;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.cyanogenmod.internal.cmparts.PartsList.ACTION_PART_CHANGED;
import static org.cyanogenmod.internal.cmparts.PartsList.EXTRA_PART;
import static org.cyanogenmod.internal.cmparts.PartsList.EXTRA_PART_KEY;

/**
 * PartsRefresher keeps remote UI clients up to date with any changes in the
 * state of the Part which should be reflected immediately. For preferences,
 * the clear use case is refreshing the summary.
 *
 * This works in conjunction with CMPartsPreference, which will send an
 * ordered broadcast requesting updated information. The part will be
 * looked up, and checked for a static SUMMARY_INFO field. If an
 * instance of SummaryInfo is found in this field, the result of the
 * broadcast will be updated with the new information.
 *
 * Parts can also call refreshPart to send an asynchronous update to any
 * active remote components via broadcast.
 */
public class PartsRefresher {

    private static final String TAG = PartsRefresher.class.getSimpleName();

    public static final String FIELD_NAME_SUMMARY_PROVIDER = "SUMMARY_PROVIDER";

    private static PartsRefresher sInstance;

    private final Context mContext;

    private final Handler mHandler;

    private final SettingsObserver mObserver;

    private PartsRefresher(Context context) {
        super();
        mContext = context;
        mHandler = new Handler();
        mObserver = new SettingsObserver();
    }

    public static synchronized PartsRefresher get(Context context) {
        if (sInstance == null) {
            sInstance = new PartsRefresher(context);
        }
        return sInstance;
    }

    private Refreshable.SummaryProvider getPartSummary(PartInfo pi) {
        final Class<?> clazz;
        try {
            clazz = Class.forName(pi.getFragmentClass());
        } catch (ClassNotFoundException e) {
            Log.d(TAG, "Cannot find class: " + pi.getFragmentClass());
            return null;
        }

        if (clazz == null || !Refreshable.class.isAssignableFrom(clazz)) {
            return null;
        }

        try {
            final Field f = clazz.getField(FIELD_NAME_SUMMARY_PROVIDER);
            return (Refreshable.SummaryProvider) f.get(null);
        } catch (Exception e) {
            // ignore
        }
        return null;
    }

    boolean updateExtras(String key, Bundle bundle) {
        final PartInfo pi = PartsList.get(mContext).getPartInfo(key);
        if (pi == null) {
            return false;
        }

        final Refreshable.SummaryProvider si = getPartSummary(pi);
        if (si == null) {
            return false;
        }

        String summary = si.getSummary(mContext, key);

        if (Objects.equals(summary, pi.getSummary())) {
            return false;
        }

        pi.setSummary(si.getSummary(mContext, key));
        bundle.putString(EXTRA_PART_KEY, key);
        bundle.putParcelable(EXTRA_PART, pi);
        return true;
    }

    public void refreshPart(String key) {
        final Intent i = new Intent(ACTION_PART_CHANGED);
        final Bundle extras = new Bundle();
        if (updateExtras(key, extras)) {
            i.putExtras(extras);
            mContext.sendBroadcastAsUser(i, UserHandle.CURRENT, Manifest.permission.MANAGE_PARTS);
        }
    }

    public void addTrigger(Refreshable listener, Uri... contentUris) {
        mObserver.register(listener, contentUris);
    }

    public void removeTrigger(Refreshable listener) {
        mObserver.unregister(listener);
    }

    public interface Refreshable {
        public void onRefresh(Context context, Uri what);

        public interface SummaryProvider {
            public String getSummary(Context context, String key);
        }
    }

    private final class SettingsObserver extends ContentObserver {

        private final Map<Refreshable, Set<Uri>> mTriggers = new ArrayMap<>();
        private final List<Uri> mRefs = new ArrayList<>();

        private final ContentResolver mResolver;

        public SettingsObserver() {
            super(mHandler);

            mResolver = mContext.getContentResolver();
        }

        public void register(Refreshable listener, Uri... contentUris) {
            synchronized (mRefs) {
                Set<Uri> uris = mTriggers.get(listener);
                if (uris == null) {
                    uris = new ArraySet<Uri>();
                    mTriggers.put(listener, uris);
                }
                for (Uri contentUri : contentUris) {
                    uris.add(contentUri);
                    if (!mRefs.contains(contentUri)) {
                        mResolver.registerContentObserver(contentUri, false, this);
                    }
                    mRefs.add(contentUri);
                }
            }
        }

        public void unregister(Refreshable listener) {
            synchronized (mRefs) {
                Set<Uri> uris = mTriggers.remove(listener);
                if (uris != null) {
                    for (Uri uri : uris) {
                        mRefs.remove(uri);
                    }
                }
                if (mRefs.size() == 0) {
                    mResolver.unregisterContentObserver(this);
                }
            }
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            synchronized (mRefs) {
                super.onChange(selfChange, uri);

                final Set<Refreshable> notify = new ArraySet<>();
                for (Map.Entry<Refreshable, Set<Uri>> entry : mTriggers.entrySet()) {
                    if (entry.getValue().contains(uri)) {
                        notify.add(entry.getKey());
                    }
                }

                for (Refreshable listener : notify) {
                    listener.onRefresh(mContext, uri);
                }
            }
        }
    }

}
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed 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.cyanogenmod.cmparts;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import org.cyanogenmod.internal.cmparts.PartsList;

import static org.cyanogenmod.internal.cmparts.PartsList.ACTION_REFRESH_PART;

public class RefreshReceiver extends BroadcastReceiver {

    /**
     * Receiver which handles clients requesting a summary update. A client may send
     * the REFERSH_PART action via sendOrderedBroadcast, and we will reply immediately.
     *
     * @param context
     * @param intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_REFRESH_PART.equals(intent.getAction()) && isOrderedBroadcast()) {
            final String key = intent.getStringExtra(PartsList.EXTRA_PART_KEY);
            if (key != null &&
                    PartsRefresher.get(context).updateExtras(key, getResultExtras(true))) {
                setResultCode(Activity.RESULT_OK);
                return;
            }
        }
        abortBroadcast();
    }
}
+27 −9
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.XmlRes;
import android.support.v14.preference.PreferenceFragment;
@@ -37,6 +38,7 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -46,13 +48,14 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.view.animation.*;

import java.util.Arrays;
import java.util.UUID;

/**
 * Base class for Settings fragments, with some helper functions and dialog management.
 */
public abstract class SettingsPreferenceFragment extends PreferenceFragment
        implements DialogCreatable {
        implements DialogCreatable, PartsRefresher.Refreshable {

    /**
     * The Help Uri Resource key. This can be passed as an extra argument when creating the
@@ -102,6 +105,8 @@ public abstract class SettingsPreferenceFragment extends PreferenceFragment
    private ArrayMap<String, Preference> mPreferenceCache;
    private boolean mAnimationAllowed;

    private final ArraySet<Uri> mTriggerUris = new ArraySet<Uri>();

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
@@ -199,6 +204,11 @@ public abstract class SettingsPreferenceFragment extends PreferenceFragment
        unregisterObserverIfNeeded();
    }

    @Override
    public void onRefresh(Context context, Uri contentUri) {
        PartsRefresher.get(context).refreshPart(getPreferenceScreen().getKey());
    }

    public void showLoadingWhenEmpty() {
        View loading = getView().findViewById(R.id.loading_container);
        setEmptyView(loading);
@@ -499,6 +509,13 @@ public abstract class SettingsPreferenceFragment extends PreferenceFragment
        return getActivity().getPackageManager();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        PartsRefresher.get(activity).addTrigger(this,
                mTriggerUris.toArray(new Uri[mTriggerUris.size()]));
    }

    @Override
    public void onDetach() {
        if (isRemoving()) {
@@ -507,9 +524,18 @@ public abstract class SettingsPreferenceFragment extends PreferenceFragment
                mDialogFragment = null;
            }
        }
        PartsRefresher.get(getActivity()).removeTrigger(this);
        super.onDetach();
    }

    protected void addTrigger(Uri... contentUris) {
        mTriggerUris.addAll(Arrays.asList(contentUris));
        if (!isDetached()) {
            PartsRefresher.get(getActivity()).addTrigger(this,
                    mTriggerUris.toArray(new Uri[mTriggerUris.size()]));
        }
    }

    // Dialog management

    protected void showDialog(int dialogId) {
@@ -736,14 +762,6 @@ public abstract class SettingsPreferenceFragment extends PreferenceFragment
        getActivity().setResult(result);
    }

    public String getDashboardTitle() {
        return null;
    }

    public String getDashboardSummary() {
        return null;
    }

    public boolean isAvailable() {
        return true;
    }
Loading