Dan Sandler | f4e83e0 | 2020-05-12 21:25:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.egg.neko; |
| 18 | |
| 19 | import android.app.Dialog; |
| 20 | import android.content.Context; |
| 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | import android.widget.ImageView; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | import androidx.annotation.NonNull; |
| 28 | import androidx.recyclerview.widget.GridLayoutManager; |
| 29 | import androidx.recyclerview.widget.RecyclerView; |
| 30 | |
| 31 | import com.android.egg.R; |
| 32 | |
| 33 | import java.util.ArrayList; |
| 34 | |
| 35 | public class NekoDialog extends Dialog { |
| 36 | |
| 37 | private final Adapter mAdapter; |
| 38 | |
| 39 | public NekoDialog(@NonNull Context context) { |
| 40 | super(context, android.R.style.Theme_Material_Dialog_NoActionBar); |
| 41 | RecyclerView view = new RecyclerView(getContext()); |
| 42 | mAdapter = new Adapter(getContext()); |
| 43 | view.setLayoutManager(new GridLayoutManager(getContext(), 2)); |
| 44 | view.setAdapter(mAdapter); |
| 45 | final float dp = context.getResources().getDisplayMetrics().density; |
| 46 | final int pad = (int)(16*dp); |
| 47 | view.setPadding(pad, pad, pad, pad); |
| 48 | setContentView(view); |
| 49 | } |
| 50 | |
| 51 | private void onFoodSelected(Food food) { |
| 52 | PrefState prefs = new PrefState(getContext()); |
| 53 | int currentState = prefs.getFoodState(); |
| 54 | if (currentState == 0 && food.getType() != 0) { |
| 55 | NekoService.registerJob(getContext(), food.getInterval(getContext())); |
| 56 | } |
| 57 | // MetricsLogger.histogram(getContext(), "egg_neko_offered_food", food.getType()); |
| 58 | prefs.setFoodState(food.getType()); |
| 59 | dismiss(); |
| 60 | } |
| 61 | |
| 62 | private class Adapter extends RecyclerView.Adapter<Holder> { |
| 63 | |
| 64 | private final Context mContext; |
| 65 | private final ArrayList<Food> mFoods = new ArrayList<>(); |
| 66 | |
| 67 | public Adapter(Context context) { |
| 68 | mContext = context; |
| 69 | int[] foods = context.getResources().getIntArray(R.array.food_names); |
| 70 | // skip food 0, you can't choose it |
| 71 | for (int i=1; i<foods.length; i++) { |
| 72 | mFoods.add(new Food(i)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Holder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 78 | return new Holder(LayoutInflater.from(parent.getContext()) |
| 79 | .inflate(R.layout.food_layout, parent, false)); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onBindViewHolder(final Holder holder, int position) { |
| 84 | final Food food = mFoods.get(position); |
| 85 | ((ImageView) holder.itemView.findViewById(R.id.icon)) |
| 86 | .setImageIcon(food.getIcon(mContext)); |
| 87 | ((TextView) holder.itemView.findViewById(R.id.text)) |
| 88 | .setText(food.getName(mContext)); |
| 89 | holder.itemView.setOnClickListener(new View.OnClickListener() { |
| 90 | @Override |
| 91 | public void onClick(View v) { |
| 92 | onFoodSelected(mFoods.get(holder.getAdapterPosition())); |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public int getItemCount() { |
| 99 | return mFoods.size(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public static class Holder extends RecyclerView.ViewHolder { |
| 104 | |
| 105 | public Holder(View itemView) { |
| 106 | super(itemView); |
| 107 | } |
| 108 | } |
| 109 | } |