1 /* 2 * Copyright (C) 2013 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.appcompat; 18 19 import android.content.res.ColorStateList; 20 import android.graphics.PorterDuff; 21 import android.graphics.drawable.Drawable; 22 import android.os.Build; 23 import android.os.Bundle; 24 import android.widget.Switch; 25 import android.widget.TextView; 26 import android.widget.Toolbar; 27 28 import androidx.appcompat.app.AppCompatActivity; 29 import androidx.appcompat.content.res.AppCompatResources; 30 31 /** 32 * No-op activity for the AppCompat Lint demo 33 */ 34 public class AppCompatLintDemo extends AppCompatActivity { 35 private class ResourceLoader { getColorStateList(int resourceId)36 private ColorStateList getColorStateList(int resourceId) { 37 return AppCompatResources.getColorStateList(AppCompatLintDemo.this, resourceId); 38 } 39 } 40 41 @Override onCreate(Bundle savedInstanceState)42 public void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 TextView noop = findViewById(R.id.noop); 46 // The following call to getColorStateList should be flagged by our Lint rule, since 47 // it's on the core Android Resources class 48 ColorStateList csl = 49 getResources().getColorStateList(R.color.color_state_list_missing_android_alpha); 50 noop.setTextColor(csl); 51 52 // The following call to getColorStateList should not be flagged by our Lint rule, since 53 // it's on our own custom inner class 54 ColorStateList csl2 = new ResourceLoader().getColorStateList( 55 R.color.color_state_list_missing_android_alpha); 56 noop.setTextColor(csl2); 57 58 Drawable dr = getResources().getDrawable(R.drawable.app_sample_code); 59 noop.setCompoundDrawables(dr, null, null, null); 60 61 if (Build.VERSION.SDK_INT >= 23) { 62 // These should be flagged to use TextViewCompat 63 noop.setCompoundDrawableTintList(csl); 64 noop.setCompoundDrawableTintMode(PorterDuff.Mode.DST); 65 } 66 67 // The following usage of the core Switch widget should be flagged by our Lint rule 68 Switch mySwitch = new Switch(this); 69 mySwitch.setChecked(true); 70 71 if (Build.VERSION.SDK_INT >= 21) { 72 // The following call should be flagged since we're extending AppCompatActivity 73 setActionBar(new Toolbar(this)); 74 } 75 } 76 } 77