summaryrefslogtreecommitdiff
path: root/sdk/src/java/lineageos/preference/RemotePreferenceUpdater.java
blob: af2b65025e8b799c44d48ab09f51c91b80da1492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * 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 lineageos.preference;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;

import java.util.Objects;

import lineageos.platform.Manifest;

/**
 * Base class for remote summary providers.
 * <p>
 * When an application is hosting preferences which are served by a different process,
 * the former needs to stay updated with changes in order to display the correct
 * summary when the user returns to the latter.
 * <p>
 * This class implements a simple ordered broadcast mechanism where the application
 * running the RemotePreference sends an explicit broadcast to the host, who
 * fills out the extras in the result bundle and returns it to the caller.
 * <p>
 * A minimal implementation will override getSummary and return a summary
 * for the given key. Alternatively, fillResultExtras can be overridden
 * if additional data should be added to the result.
 */
public class RemotePreferenceUpdater extends BroadcastReceiver {

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

    private static final boolean DEBUG = Log.isLoggable(
            RemotePreference.class.getSimpleName(), Log.VERBOSE);

    private static Intent getTargetIntent(Context context, String key) {
        final Intent i = new Intent(RemotePreference.ACTION_REFRESH_PREFERENCE);
        i.putExtra(RemotePreference.EXTRA_KEY, key);
        return i;
    }

    /**
     * Fetch the updated summary for the given key
     *
     * @param key
     * @return the summary for the given key
     */
    protected String getSummary(Context context, String key) {
        return null;
    }


    /**
     * Fill the bundle with the summary and any other data needed  to update
     * the client.
     *
     * @param context
     * @param key
     * @param extras
     * @return true if successful
     */
    protected boolean fillResultExtras(Context context, String key, Bundle extras) {
        extras.putString(RemotePreference.EXTRA_KEY, key);

        final String summary = getSummary(context, key);
        if (summary == null) {
            return false;
        }

        extras.putString(RemotePreference.EXTRA_SUMMARY, summary);
        return true;
    }


    /**
     * @hide
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        if (isOrderedBroadcast() &&
                RemotePreference.ACTION_UPDATE_PREFERENCE.equals(intent.getAction())) {
            final String key = intent.getStringExtra(RemotePreference.EXTRA_KEY);
            if (DEBUG) Log.d(TAG, "onReceive key=" +key +
                       " intent=" + Objects.toString(intent) +
                       " extras=" + Objects.toString(intent.getExtras()));

            if (key != null) {
                if (fillResultExtras(context, key, getResultExtras(true))) {
                    setResultCode(Activity.RESULT_OK);

                    if (DEBUG) Log.d(TAG, "onReceive result=" +
                               Objects.toString(getResultExtras(true)));
                    return;
                }
            }
            abortBroadcast();
        }
    }

    /**
     * Tell the RemotePreference that updated state is available. Call from
     * the fragment when necessary.
     *
     * @param context
     * @param key
     */
    public static void notifyChanged(Context context, String key) {
        if (DEBUG) Log.d(TAG, "notifyChanged: key=" + key +
                   " target=" + Objects.toString(getTargetIntent(context, key)));
        context.sendBroadcastAsUser(getTargetIntent(context, key),
                UserHandle.CURRENT, Manifest.permission.MANAGE_REMOTE_PREFERENCES);
    }
}