1 /*
2  * Copyright (C) 2016 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.android.cts.verifier.managedprovisioning;
18 
19 import android.app.ActionBar;
20 import android.app.Activity;
21 import android.app.admin.DevicePolicyManager;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.EditText;
27 
28 import com.android.cts.verifier.R;
29 
30 public class SetSupportMessageActivity extends Activity implements View.OnClickListener {
31     public static final String ACTION_SET_SUPPORT_MSG =
32             "com.android.cts.verifier.managedprovisioning.action.SET_SUPPORT_MSG";
33     public static final String EXTRA_SUPPORT_MSG_TYPE =
34             "com.android.cts.verifier.managedprovisioning.extra.SUPPORT_MSG_TYPE";
35 
36     public static final String TYPE_SHORT_MSG = "short-msg";
37     public static final String TYPE_LONG_MSG = "long-msg";
38 
39     private String mType;
40     private EditText mSupportMessage;
41     private DevicePolicyManager mDpm;
42     private ComponentName mAdmin;
43 
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.set_support_message);
47         findViewById(R.id.set_default_message).setOnClickListener(this);
48         findViewById(R.id.set_message).setOnClickListener(this);
49         findViewById(R.id.clear_message).setOnClickListener(this);
50         mSupportMessage = findViewById(R.id.message_view);
51 
52         ActionBar actBar = getActionBar();
53         if (actBar != null) {
54             actBar.setDisplayHomeAsUpEnabled(true);
55         }
56 
57         mType = getIntent().getStringExtra(EXTRA_SUPPORT_MSG_TYPE);
58         setTitle(TYPE_SHORT_MSG.equals(mType)
59                 ? R.string.policy_transparency_short_support_msg_label
60                 : R.string.policy_transparency_long_support_msg_label);
61         mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
62         mAdmin = DeviceAdminTestReceiver.getReceiverComponentName();
63 
64         mSupportMessage.setText(TYPE_SHORT_MSG.equals(mType)
65                 ? mDpm.getShortSupportMessage(mAdmin)
66                 : mDpm.getLongSupportMessage(mAdmin));
67     }
68 
69     @Override
onClick(View view)70     public void onClick(View view) {
71         String message = null;
72         switch (view.getId()) {
73             case R.id.set_default_message: {
74                 message = getString(TYPE_SHORT_MSG.equals(mType)
75                         ? R.string.policy_transparency_default_short_msg
76                         : R.string.policy_transparency_default_long_msg);
77             } break;
78             case R.id.set_message: {
79                 message = mSupportMessage.getText().toString();
80             } break;
81             case R.id.clear_message: {
82                 message = null;
83             } break;
84         }
85         if (TYPE_SHORT_MSG.equals(mType)) {
86             mDpm.setShortSupportMessage(mAdmin, message);
87         } else {
88             mDpm.setLongSupportMessage(mAdmin, message);
89         }
90         mSupportMessage.setText(message);
91     }
92 }