aboutsummaryrefslogtreecommitdiff
path: root/src/android_system_settings/src/com/android/angle/common/Receiver.java
blob: 03fbf8d1ed5293e781fc201099f9599cb975de6b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright 2019 The Android Open Source 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 com.android.angle.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;

import androidx.preference.PreferenceManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

public class Receiver extends BroadcastReceiver
{

    private static final String TAG              = "AngleReceiver";
    private static final String ANGLE_RULES_FILE = "a4a_rules.json";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        Log.v(TAG, "Received intent: '" + action + "'");

        if (action.equals(context.getString(R.string.intent_angle_for_android_toast_message)))
        {
            Bundle results = getResultExtras(true);
            results.putString(context.getString(R.string.intent_key_a4a_toast_message),
                    context.getString(R.string.angle_in_use_toast_message));
        }
        else
        {
            String jsonStr      = loadRules(context);
            String packageNames = parsePackageNames(jsonStr);

            // Update the ANGLE allowlist
            if (packageNames != null)
            {
                GlobalSettings.updateAngleAllowlist(context, packageNames);
            }

            updateDeveloperOptionsWatcher(context);
        }
    }

    /*
     * Open the rules file and pull all the JSON into a string
     */
    private String loadRules(Context context)
    {
        String jsonStr = null;

        try
        {
            InputStream rulesStream = context.getAssets().open(ANGLE_RULES_FILE);
            int size                = rulesStream.available();
            byte[] buffer           = new byte[size];
            rulesStream.read(buffer);
            rulesStream.close();
            jsonStr = new String(buffer, "UTF-8");
        }
        catch (IOException ioe)
        {
            Log.e(TAG, "Failed to open " + ANGLE_RULES_FILE + ": ", ioe);
        }

        return jsonStr;
    }

    /*
     * Extract all app package names from the json file and return them comma separated
     */
    private String parsePackageNames(String rulesJSON)
    {
        StringBuilder packageNames = new StringBuilder();

        try
        {
            JSONObject jsonObj = new JSONObject(rulesJSON);
            JSONArray rules    = jsonObj.getJSONArray("Rules");
            if (rules == null)
            {
                Log.e(TAG, "No Rules in " + ANGLE_RULES_FILE);
                return null;
            }
            for (int i = 0; i < rules.length(); i++)
            {
                JSONObject rule = rules.getJSONObject(i);
                JSONArray apps  = rule.optJSONArray("Applications");
                if (apps == null)
                {
                    Log.v(TAG, "Skipping Rules entry with no Applications");
                    continue;
                }
                for (int j = 0; j < apps.length(); j++)
                {
                    JSONObject app = apps.optJSONObject(j);
                    String appName = app.optString("AppName");
                    if ((appName == null) || appName.isEmpty())
                    {
                        Log.e(TAG, "Invalid AppName: '" + appName + "'");
                    }
                    if (!packageNames.toString().isEmpty())
                    {
                        packageNames.append(",");
                    }
                    packageNames.append(appName);
                }
            }
            Log.v(TAG, "Parsed the following package names from " + ANGLE_RULES_FILE + ": "
                               + packageNames);
        }
        catch (JSONException je)
        {
            Log.e(TAG, "Error when parsing angle JSON: ", je);
            return null;
        }

        return packageNames.toString();
    }

    static void updateAllUseAngle(Context context)
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String allUseAngleKey   = context.getString(R.string.pref_key_all_angle);
        boolean allUseAngle     = prefs.getBoolean(allUseAngleKey, false);

        GlobalSettings.updateAllUseAngle(context, allUseAngle);

        Log.v(TAG, "All PKGs use ANGLE set to: " + allUseAngle);
    }

    static void updateShowAngleInUseDialogBox(Context context)
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String showAngleInUseDialogBoxKey =
                context.getString(R.string.pref_key_angle_in_use_dialog);
        boolean showAngleInUseDialogBox = prefs.getBoolean(showAngleInUseDialogBoxKey, false);

        GlobalSettings.updateShowAngleInUseDialog(context, showAngleInUseDialogBox);

        Log.v(TAG, "Show 'ANGLE In Use' dialog box set to: " + showAngleInUseDialogBox);
    }

    /**
     * When Developer Options are disabled, reset all of the global settings back to their defaults.
     */
    private static void updateDeveloperOptionsWatcher(Context context)
    {
        Uri settingUri = Settings.Global.getUriFor(Settings.Global.DEVELOPMENT_SETTINGS_ENABLED);

        ContentObserver developerOptionsObserver = new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange)
            {
                super.onChange(selfChange);

                boolean developerOptionsEnabled =
                        (1
                                == Settings.Global.getInt(context.getContentResolver(),
                                        Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0));

                Log.v(TAG, "Developer Options enabled value changed: "
                                   + "developerOptionsEnabled = " + developerOptionsEnabled);

                if (!developerOptionsEnabled)
                {
                    // Reset the necessary settings to their defaults.
                    SharedPreferences.Editor editor =
                            PreferenceManager.getDefaultSharedPreferences(context).edit();
                    editor.clear();
                    editor.apply();
                    GlobalSettings.clearAllGlobalSettings(context);
                }
            }
        };

        context.getContentResolver().registerContentObserver(
                settingUri, false, developerOptionsObserver);
        developerOptionsObserver.onChange(true);
    }
}