Skip to content

Most visited

Recently visited

navigation
AppRestrictionSchema / src / com.example.android.apprestrictionschema /

AppRestrictionSchemaFragment.java

1
/*
2
 * Copyright (C) 2014 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.apprestrictionschema;
18
 
19
import android.content.Context;
20
import android.content.RestrictionEntry;
21
import android.content.RestrictionsManager;
22
import android.os.Build;
23
import android.os.Bundle;
24
import android.os.Parcelable;
25
import android.support.annotation.Nullable;
26
import android.support.v4.app.Fragment;
27
import android.text.TextUtils;
28
import android.view.LayoutInflater;
29
import android.view.View;
30
import android.view.ViewGroup;
31
import android.widget.Button;
32
import android.widget.TextView;
33
import android.widget.Toast;
34
 
35
import com.example.android.common.logger.Log;
36
 
37
import java.util.List;
38
 
39
/**
40
 * Pressing the button on this fragment pops up a simple Toast message. The button is enabled or
41
 * disabled according to the restrictions set by device/profile owner. You can use the
42
 * AppRestrictionEnforcer sample as a profile owner for this.
43
 */
44
public class AppRestrictionSchemaFragment extends Fragment implements View.OnClickListener {
45
 
46
    // Tag for the logger
47
    private static final String TAG = "AppRestrictionSchemaFragment";
48
 
49
    private static final String KEY_CAN_SAY_HELLO = "can_say_hello";
50
    private static final String KEY_MESSAGE = "message";
51
    private static final String KEY_NUMBER = "number";
52
    private static final String KEY_RANK = "rank";
53
    private static final String KEY_APPROVALS = "approvals";
54
    private static final String KEY_ITEMS = "items";
55
    private static final String KEY_ITEM_KEY = "key";
56
    private static final String KEY_ITEM_VALUE = "value";
57
 
58
    private static final boolean BUNDLE_SUPPORTED = Build.VERSION.SDK_INT >= 23;
59
 
60
    // Message to show when the button is clicked (String restriction)
61
    private String mMessage;
62
 
63
    // UI Components
64
    private TextView mTextSayHello;
65
    private Button mButtonSayHello;
66
    private TextView mTextNumber;
67
    private TextView mTextRank;
68
    private TextView mTextApprovals;
69
    private TextView mTextItems;
70
 
71
    @Override
72
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
73
                             @Nullable Bundle savedInstanceState) {
74
        return inflater.inflate(R.layout.fragment_app_restriction_schema, container, false);
75
    }
76
 
77
    @Override
78
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
79
        mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation);
80
        mButtonSayHello = (Button) view.findViewById(R.id.say_hello);
81
        mTextNumber = (TextView) view.findViewById(R.id.your_number);
82
        mTextRank = (TextView) view.findViewById(R.id.your_rank);
83
        mTextApprovals = (TextView) view.findViewById(R.id.approvals_you_have);
84
        View bundleSeparator = view.findViewById(R.id.bundle_separator);
85
        View bundleArraySeparator = view.findViewById(R.id.bundle_array_separator);
86
        mTextItems = (TextView) view.findViewById(R.id.your_items);
87
        mButtonSayHello.setOnClickListener(this);
88
        if (BUNDLE_SUPPORTED) {
89
            bundleSeparator.setVisibility(View.VISIBLE);
90
            bundleArraySeparator.setVisibility(View.VISIBLE);
91
            mTextItems.setVisibility(View.VISIBLE);
92
        } else {
93
            bundleSeparator.setVisibility(View.GONE);
94
            bundleArraySeparator.setVisibility(View.GONE);
95
            mTextItems.setVisibility(View.GONE);
96
        }
97
    }
98
 
99
    @Override
100
    public void onResume() {
101
        super.onResume();
102
        resolveRestrictions();
103
    }
104
 
105
    private void resolveRestrictions() {
106
        RestrictionsManager manager =
107
                (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
108
        Bundle restrictions = manager.getApplicationRestrictions();
109
        List<RestrictionEntry> entries = manager.getManifestRestrictions(
110
                getActivity().getApplicationContext().getPackageName());
111
        for (RestrictionEntry entry : entries) {
112
            String key = entry.getKey();
113
            Log.d(TAG, "key: " + key);
114
            if (key.equals(KEY_CAN_SAY_HELLO)) {
115
                updateCanSayHello(entry, restrictions);
116
            } else if (key.equals(KEY_MESSAGE)) {
117
                updateMessage(entry, restrictions);
118
            } else if (key.equals(KEY_NUMBER)) {
119
                updateNumber(entry, restrictions);
120
            } else if (key.equals(KEY_RANK)) {
121
                updateRank(entry, restrictions);
122
            } else if (key.equals(KEY_APPROVALS)) {
123
                updateApprovals(entry, restrictions);
124
            } else if (key.equals(KEY_ITEMS)) {
125
                updateItems(restrictions);
126
            }
127
        }
128
    }
129
 
130
    private void updateCanSayHello(RestrictionEntry entry, Bundle restrictions) {
131
        boolean canSayHello;
132
        if (restrictions == null || !restrictions.containsKey(KEY_CAN_SAY_HELLO)) {
133
            canSayHello = entry.getSelectedState();
134
        } else {
135
            canSayHello = restrictions.getBoolean(KEY_CAN_SAY_HELLO);
136
        }
137
        mTextSayHello.setText(canSayHello ?
138
                R.string.explanation_can_say_hello_true :
139
                R.string.explanation_can_say_hello_false);
140
        mButtonSayHello.setEnabled(canSayHello);
141
    }
142
 
143
    private void updateMessage(RestrictionEntry entry, Bundle restrictions) {
144
        if (restrictions == null || !restrictions.containsKey(KEY_MESSAGE)) {
145
            mMessage = entry.getSelectedString();
146
        } else {
147
            mMessage = restrictions.getString(KEY_MESSAGE);
148
        }
149
    }
150
 
151
    private void updateNumber(RestrictionEntry entry, Bundle restrictions) {
152
        int number;
153
        if (restrictions == null || !restrictions.containsKey(KEY_NUMBER)) {
154
            number = entry.getIntValue();
155
        } else {
156
            number = restrictions.getInt(KEY_NUMBER);
157
        }
158
        mTextNumber.setText(getString(R.string.your_number, number));
159
    }
160
 
161
    private void updateRank(RestrictionEntry entry, Bundle restrictions) {
162
        String rank;
163
        if (restrictions == null || !restrictions.containsKey(KEY_RANK)) {
164
            rank = entry.getSelectedString();
165
        } else {
166
            rank = restrictions.getString(KEY_RANK);
167
        }
168
        mTextRank.setText(getString(R.string.your_rank, rank));
169
    }
170
 
171
    private void updateApprovals(RestrictionEntry entry, Bundle restrictions) {
172
        String[] approvals;
173
        if (restrictions == null || !restrictions.containsKey(KEY_APPROVALS)) {
174
            approvals = entry.getAllSelectedStrings();
175
        } else {
176
            approvals = restrictions.getStringArray(KEY_APPROVALS);
177
        }
178
        String text;
179
        if (approvals == null || approvals.length == 0) {
180
            text = getString(R.string.none);
181
        } else {
182
            text = TextUtils.join(", ", approvals);
183
        }
184
        mTextApprovals.setText(getString(R.string.approvals_you_have, text));
185
    }
186
 
187
    private void updateItems(Bundle restrictions) {
188
        if (!BUNDLE_SUPPORTED) {
189
            return;
190
        }
191
        StringBuilder builder = new StringBuilder();
192
        if (restrictions != null) {
193
            Parcelable[] parcelables = restrictions.getParcelableArray(KEY_ITEMS);
194
            if (parcelables != null && parcelables.length > 0) {
195
                Bundle[] items = new Bundle[parcelables.length];
196
                for (int i = 0; i < parcelables.length; i++) {
197
                    items[i] = (Bundle) parcelables[i];
198
                }
199
                boolean first = true;
200
                for (Bundle item : items) {
201
                    if (!item.containsKey(KEY_ITEM_KEY) || !item.containsKey(KEY_ITEM_VALUE)) {
202
                        continue;
203
                    }
204
                    if (first) {
205
                        first = false;
206
                    } else {
207
                        builder.append(", ");
208
                    }
209
                    builder.append(item.getString(KEY_ITEM_KEY));
210
                    builder.append(":");
211
                    builder.append(item.getString(KEY_ITEM_VALUE));
212
                }
213
            } else {
214
                builder.append(getString(R.string.none));
215
            }
216
        } else {
217
            builder.append(getString(R.string.none));
218
        }
219
        mTextItems.setText(getString(R.string.your_items, builder));
220
    }
221
 
222
    @Override
223
    public void onClick(View view) {
224
        switch (view.getId()) {
225
            case R.id.say_hello: {
226
                Toast.makeText(getActivity(), getString(R.string.message, mMessage),
227
                        Toast.LENGTH_SHORT).show();
228
                break;
229
            }
230
        }
231
    }
232
 
233
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.