1 /*
2  * Copyright (C) 2019 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.car.themeplayground;
18 
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.widget.ArrayAdapter;
22 import android.widget.AutoCompleteTextView;
23 import android.widget.Button;
24 import android.widget.EditText;
25 import android.widget.Toast;
26 
27 import java.io.BufferedReader;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Activity that shows different device default themes. Auto complete themes come from
35  * theme_names.txt file in the raw folder. User can also input a valid theme in the textbox even if
36  * the theme is not available in the auto-complete.
37  */
38 public class DefaultThemeSamples extends AbstractSampleActivity {
39 
40     private static final String TAG = DefaultThemeSamples.class.getSimpleName();
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         Utils.onActivityCreateSetTheme(this);
46         setContentView(R.layout.device_default_theme_samples);
47         Button buttonApply = findViewById(R.id.button_apply);
48         AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.theme_name);
49         ArrayAdapter<String> adapter =
50                 new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
51                         listAllThemes());
52         textView.setAdapter(adapter);
53         buttonApply.setOnClickListener(v -> {
54             EditText input = findViewById(R.id.theme_name);
55             String themeName = input.getText().toString();
56             int themeResId = this.getResources().getIdentifier(themeName,
57                     "style", "android");
58             if (themeResId == 0) {
59                 Toast.makeText(this, "No such theme found. ",
60                         Toast.LENGTH_SHORT).show();
61                 return;
62             }
63             Toast.makeText(this, "Applying theme: " + themeName,
64                     Toast.LENGTH_SHORT).show();
65             Utils.changeToTheme(this, themeName, themeResId);
66         });
67     }
68 
listAllThemes()69     private String[] listAllThemes() {
70         String data;
71         List<String> list = new ArrayList<>();
72         try (InputStream is = this.getResources().openRawResource(R.raw.theme_names)) {
73             BufferedReader reader = new BufferedReader(new InputStreamReader(is));
74             while ((data = reader.readLine()) != null) {
75                 list.add(data);
76             }
77         } catch (Exception e) {
78             Log.e(TAG, "Failed listing theme names", e);
79         }
80         return list.toArray(new String[0]);
81     }
82 }
83