aboutsummaryrefslogtreecommitdiff
path: root/admin/AppRestrictionSchema/Application/src/main/java/com/example/android/apprestrictionschema/AppRestrictionSchemaFragment.java
blob: 8d219656a63f905102b67b5be588bcd00489ec5b (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (C) 2014 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.example.android.apprestrictionschema;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.RestrictionEntry;
import android.content.RestrictionsManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.common.logger.Log;

import java.util.List;

/**
 * Pressing the button on this fragment pops up a simple Toast message. The button is enabled or
 * disabled according to the restrictions set by device/profile owner. You can use the
 * AppRestrictionEnforcer sample as a profile owner for this.
 */
public class AppRestrictionSchemaFragment extends Fragment implements View.OnClickListener {

    // Tag for the logger
    private static final String TAG = "AppRestrictionSchema";

    private static final String KEY_CAN_SAY_HELLO = "can_say_hello";
    private static final String KEY_MESSAGE = "message";
    private static final String KEY_NUMBER = "number";
    private static final String KEY_RANK = "rank";
    private static final String KEY_APPROVALS = "approvals";
    private static final String KEY_ITEMS = "items";
    private static final String KEY_ITEM_KEY = "key";
    private static final String KEY_ITEM_VALUE = "value";

    private static final boolean BUNDLE_SUPPORTED = Build.VERSION.SDK_INT >= 23;

    // Message to show when the button is clicked (String restriction)
    private String mMessage;

    // Observes restriction changes
    private BroadcastReceiver mBroadcastReceiver;

    // UI Components
    private TextView mTextSayHello;
    private Button mButtonSayHello;
    private TextView mTextNumber;
    private TextView mTextRank;
    private TextView mTextApprovals;
    private TextView mTextItems;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_app_restriction_schema, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation);
        mButtonSayHello = (Button) view.findViewById(R.id.say_hello);
        mTextNumber = (TextView) view.findViewById(R.id.your_number);
        mTextRank = (TextView) view.findViewById(R.id.your_rank);
        mTextApprovals = (TextView) view.findViewById(R.id.approvals_you_have);
        mTextItems = (TextView) view.findViewById(R.id.your_items);
        mButtonSayHello.setOnClickListener(this);
        if (BUNDLE_SUPPORTED) {
            mTextItems.setVisibility(View.VISIBLE);
        } else {
            mTextItems.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        resolveRestrictions();
    }

    @Override
    public void onStart() {
        super.onStart();
        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                resolveRestrictions();
            }
        };
        getActivity().registerReceiver(mBroadcastReceiver,
                new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED));
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mBroadcastReceiver != null) {
            getActivity().unregisterReceiver(mBroadcastReceiver);
            mBroadcastReceiver = null;
        }
    }

    private void resolveRestrictions() {
        RestrictionsManager manager =
                (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
        Bundle restrictions = manager.getApplicationRestrictions();
        List<RestrictionEntry> entries = manager.getManifestRestrictions(
                getActivity().getApplicationContext().getPackageName());
        for (RestrictionEntry entry : entries) {
            String key = entry.getKey();
            Log.d(TAG, "key: " + key);
            if (key.equals(KEY_CAN_SAY_HELLO)) {
                updateCanSayHello(entry, restrictions);
            } else if (key.equals(KEY_MESSAGE)) {
                updateMessage(entry, restrictions);
            } else if (key.equals(KEY_NUMBER)) {
                updateNumber(entry, restrictions);
            } else if (key.equals(KEY_RANK)) {
                updateRank(entry, restrictions);
            } else if (key.equals(KEY_APPROVALS)) {
                updateApprovals(entry, restrictions);
            } else if (key.equals(KEY_ITEMS)) {
                updateItems(restrictions);
            }
        }
    }

    private void updateCanSayHello(RestrictionEntry entry, Bundle restrictions) {
        boolean canSayHello;
        if (restrictions == null || !restrictions.containsKey(KEY_CAN_SAY_HELLO)) {
            canSayHello = entry.getSelectedState();
        } else {
            canSayHello = restrictions.getBoolean(KEY_CAN_SAY_HELLO);
        }
        mTextSayHello.setText(canSayHello ?
                R.string.explanation_can_say_hello_true :
                R.string.explanation_can_say_hello_false);
        mButtonSayHello.setEnabled(canSayHello);
    }

    private void updateMessage(RestrictionEntry entry, Bundle restrictions) {
        if (restrictions == null || !restrictions.containsKey(KEY_MESSAGE)) {
            mMessage = entry.getSelectedString();
        } else {
            mMessage = restrictions.getString(KEY_MESSAGE);
        }
    }

    private void updateNumber(RestrictionEntry entry, Bundle restrictions) {
        int number;
        if (restrictions == null || !restrictions.containsKey(KEY_NUMBER)) {
            number = entry.getIntValue();
        } else {
            number = restrictions.getInt(KEY_NUMBER);
        }
        mTextNumber.setText(getString(R.string.your_number, number));
    }

    private void updateRank(RestrictionEntry entry, Bundle restrictions) {
        String rank;
        if (restrictions == null || !restrictions.containsKey(KEY_RANK)) {
            rank = entry.getSelectedString();
        } else {
            rank = restrictions.getString(KEY_RANK);
        }
        mTextRank.setText(getString(R.string.your_rank, rank));
    }

    private void updateApprovals(RestrictionEntry entry, Bundle restrictions) {
        String[] approvals;
        if (restrictions == null || !restrictions.containsKey(KEY_APPROVALS)) {
            approvals = entry.getAllSelectedStrings();
        } else {
            approvals = restrictions.getStringArray(KEY_APPROVALS);
        }
        String text;
        if (approvals == null || approvals.length == 0) {
            text = getString(R.string.none);
        } else {
            text = TextUtils.join(", ", approvals);
        }
        mTextApprovals.setText(getString(R.string.approvals_you_have, text));
    }

    private void updateItems(Bundle restrictions) {
        if (!BUNDLE_SUPPORTED) {
            return;
        }
        StringBuilder builder = new StringBuilder();
        if (restrictions != null) {
            Parcelable[] parcelables = restrictions.getParcelableArray(KEY_ITEMS);
            if (parcelables != null && parcelables.length > 0) {
                Bundle[] items = new Bundle[parcelables.length];
                for (int i = 0; i < parcelables.length; i++) {
                    items[i] = (Bundle) parcelables[i];
                }
                boolean first = true;
                for (Bundle item : items) {
                    if (!item.containsKey(KEY_ITEM_KEY) || !item.containsKey(KEY_ITEM_VALUE)) {
                        continue;
                    }
                    if (first) {
                        first = false;
                    } else {
                        builder.append(", ");
                    }
                    builder.append(item.getString(KEY_ITEM_KEY));
                    builder.append(":");
                    builder.append(item.getString(KEY_ITEM_VALUE));
                }
            } else {
                builder.append(getString(R.string.none));
            }
        } else {
            builder.append(getString(R.string.none));
        }
        mTextItems.setText(getString(R.string.your_items, builder));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.say_hello: {
                Toast.makeText(getActivity(), getString(R.string.message, mMessage),
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }

}