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.RestrictionsManager;
21
import android.os.Bundle;
22
import android.support.annotation.Nullable;
23
import android.support.v4.app.Fragment;
24
import android.view.LayoutInflater;
25
import android.view.View;
26
import android.view.ViewGroup;
27
import android.widget.Button;
28
import android.widget.TextView;
29
import android.widget.Toast;
30
 
31
import com.example.android.common.logger.Log;
32
 
33
/**
34
 * Pressing the button on this fragment pops up a simple Toast message. The button is enabled or
35
 * disabled according to the restrictions set by device/profile owner. You can use the
36
 * AppRestrictionEnforcer sample as a profile owner for this.
37
 */
38
public class AppRestrictionSchemaFragment extends Fragment implements View.OnClickListener {
39
 
40
    // Tag for the logger
41
    private static final String TAG = "AppRestrictionSchemaFragment";
42
 
43
    // UI Components
44
    private TextView mTextSayHello;
45
    private Button mButtonSayHello;
46
 
47
    @Override
48
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
49
                             @Nullable Bundle savedInstanceState) {
50
        return inflater.inflate(R.layout.fragment_app_restriction_schema, container, false);
51
    }
52
 
53
    @Override
54
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
55
        mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation);
56
        mButtonSayHello = (Button) view.findViewById(R.id.say_hello);
57
        mButtonSayHello.setOnClickListener(this);
58
    }
59
 
60
    @Override
61
    public void onResume() {
62
        super.onResume();
63
        // Update the UI according to the configured restrictions
64
        RestrictionsManager restrictionsManager =
65
                (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
66
        Bundle restrictions = restrictionsManager.getApplicationRestrictions();
67
        updateUI(restrictions);
68
    }
69
 
70
    private void updateUI(Bundle restrictions) {
71
        if (canSayHello(restrictions)) {
72
            mTextSayHello.setText(R.string.explanation_can_say_hello_true);
73
            mButtonSayHello.setEnabled(true);
74
        } else {
75
            mTextSayHello.setText(R.string.explanation_can_say_hello_false);
76
            mButtonSayHello.setEnabled(false);
77
        }
78
    }
79
 
80
    /**
81
     * Returns the current status of the restriction.
82
     *
83
     * @param restrictions The application restrictions
84
     * @return True if the app is allowed to say hello
85
     */
86
    private boolean canSayHello(Bundle restrictions) {
87
        final boolean defaultValue = false;
88
        boolean canSayHello = restrictions == null ? defaultValue :
89
                restrictions.getBoolean("can_say_hello", defaultValue);
90
        Log.d(TAG, "canSayHello: " + canSayHello);
91
        return canSayHello;
92
    }
93
 
94
    @Override
95
    public void onClick(View view) {
96
        switch (view.getId()) {
97
            case R.id.say_hello: {
98
                Toast.makeText(getActivity(), R.string.message_hello, Toast.LENGTH_SHORT).show();
99
                break;
100
            }
101
        }
102
    }
103
 
104
}