1 /*
2  * Copyright (C) 2017 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.support.text.emoji;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.support.annotation.RequiresApi;
22 import android.util.AttributeSet;
23 import android.view.LayoutInflater;
24 import android.widget.CompoundButton;
25 import android.widget.LinearLayout;
26 import android.widget.Switch;
27 
28 /**
29  * Layout that includes configuration parameters.
30  */
31 public class ConfigLayout extends LinearLayout {
32     private Switch mEnableEmojiCompat;
33     private Switch mReplaceAll;
34     private Switch mDownloadable;
35     private Switch mIndicator;
36 
ConfigLayout(Context context)37     public ConfigLayout(Context context) {
38         super(context);
39         init(context);
40     }
41 
ConfigLayout(Context context, AttributeSet attrs)42     public ConfigLayout(Context context, AttributeSet attrs) {
43         super(context, attrs);
44         init(context);
45     }
46 
47     @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
ConfigLayout(Context context, AttributeSet attrs, int defStyleAttr)48     public ConfigLayout(Context context, AttributeSet attrs,
49             int defStyleAttr) {
50         super(context, attrs, defStyleAttr);
51         init(context);
52     }
53 
54     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
ConfigLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)55     public ConfigLayout(Context context, AttributeSet attrs,
56             int defStyleAttr, int defStyleRes) {
57         super(context, attrs, defStyleAttr, defStyleRes);
58         init(context);
59     }
60 
init(Context context)61     private void init(Context context) {
62         setOrientation(VERTICAL);
63         LayoutInflater.from(context).inflate(R.layout.layout_config, this, true);
64 
65         mEnableEmojiCompat = findViewById(R.id.enable);
66         mEnableEmojiCompat.setChecked(Config.get().isCompatEnabled());
67         mEnableEmojiCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
68             @Override
69             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
70                 post(new Runnable() {
71                     @Override
72                     public void run() {
73                         fireListener();
74                     }
75                 });
76             }
77         });
78 
79         mReplaceAll = findViewById(R.id.replaceAll);
80         mReplaceAll.setChecked(Config.get().isReplaceAll());
81         mReplaceAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
82             @Override
83             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
84                 post(new Runnable() {
85                     @Override
86                     public void run() {
87                         fireListener();
88                     }
89                 });
90             }
91         });
92 
93         mDownloadable = findViewById(R.id.useDownloadable);
94         mDownloadable.setChecked(Config.get().isDownloadable());
95         mDownloadable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
96             @Override
97             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
98                 post(new Runnable() {
99                     @Override
100                     public void run() {
101                         fireListener();
102                     }
103                 });
104             }
105         });
106 
107         mIndicator = findViewById(R.id.indicator);
108         mIndicator.setChecked(Config.get().isIndicator());
109         mIndicator.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
110             @Override
111             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
112                 post(new Runnable() {
113                     @Override
114                     public void run() {
115                         fireListener();
116                     }
117                 });
118             }
119         });
120 
121     }
122 
fireListener()123     void fireListener() {
124         Config.get().update(mEnableEmojiCompat.isChecked(), mReplaceAll.isChecked(),
125                 mDownloadable.isChecked(), mIndicator.isChecked());
126     }
127 
128 }
129