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