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.settings.password;
18 
19 import android.content.Context;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
24 
25 import androidx.annotation.NonNull;
26 import androidx.recyclerview.widget.RecyclerView;
27 
28 import com.android.settings.R;
29 import com.android.settings.password.PasswordRequirementAdapter.PasswordRequirementViewHolder;
30 
31 /**
32  * Used in {@link ChooseLockPassword} to show password requirements.
33  */
34 public class PasswordRequirementAdapter extends
35         RecyclerView.Adapter<PasswordRequirementViewHolder> {
36 
37     private String[] mRequirements;
38     private Context mContext;
39 
PasswordRequirementAdapter(Context context)40     public PasswordRequirementAdapter(Context context) {
41         mContext = context;
42         setHasStableIds(true);
43     }
44 
45     @Override
onCreateViewHolder(ViewGroup parent, int viewType)46     public PasswordRequirementViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
47         View v = LayoutInflater.from(parent.getContext())
48                 .inflate(R.layout.password_requirement_item, parent, false);
49         return new PasswordRequirementViewHolder(v);
50     }
51 
52     @Override
getItemCount()53     public int getItemCount() {
54         return  mRequirements.length;
55     }
56 
setRequirements(String[] requirements)57     public void setRequirements(String[] requirements) {
58         mRequirements = requirements;
59         notifyDataSetChanged();
60     }
61 
62     @Override
getItemId(int position)63     public long getItemId(int position) {
64         return mRequirements[position].hashCode();
65     }
66 
67     @Override
onViewAttachedToWindow(@onNull PasswordRequirementViewHolder holder)68     public void onViewAttachedToWindow(@NonNull PasswordRequirementViewHolder holder) {
69         holder.mDescriptionText.announceForAccessibility(holder.mDescriptionText.getText());
70     }
71 
72     @Override
onBindViewHolder(PasswordRequirementViewHolder holder, int position)73     public void onBindViewHolder(PasswordRequirementViewHolder holder, int position) {
74         final int fontSize = mContext.getResources().getDimensionPixelSize(
75                 R.dimen.password_requirement_font_size);
76         holder.mDescriptionText.setText(mRequirements[position]);
77         holder.mDescriptionText.setTextAppearance(R.style.ScreenLockPasswordHintTextFontStyle);
78         holder.mDescriptionText.setTextSize(fontSize / mContext.getResources()
79                 .getDisplayMetrics().scaledDensity);
80     }
81 
82     public static class PasswordRequirementViewHolder extends RecyclerView.ViewHolder {
83         private TextView mDescriptionText;
84 
PasswordRequirementViewHolder(View itemView)85         public PasswordRequirementViewHolder(View itemView) {
86             super(itemView);
87             mDescriptionText = (TextView) itemView;
88         }
89     }
90 
91 }
92