1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.egg.neko;
16
17import android.support.annotation.NonNull;
18import android.app.Dialog;
19import android.content.Context;
20import android.support.v7.widget.GridLayoutManager;
21import android.support.v7.widget.RecyclerView;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ImageView;
26import android.widget.TextView;
27
28import com.android.egg.R;
29import com.android.internal.logging.MetricsLogger;
30
31import java.util.ArrayList;
32
33public class NekoDialog extends Dialog {
34
35    private final Adapter mAdapter;
36
37    public NekoDialog(@NonNull Context context) {
38        super(context, android.R.style.Theme_Material_Dialog_NoActionBar);
39        RecyclerView view = new RecyclerView(getContext());
40        mAdapter = new Adapter(getContext());
41        view.setLayoutManager(new GridLayoutManager(getContext(), 2));
42        view.setAdapter(mAdapter);
43        final float dp = context.getResources().getDisplayMetrics().density;
44        final int pad = (int)(16*dp);
45        view.setPadding(pad, pad, pad, pad);
46        setContentView(view);
47    }
48
49    private void onFoodSelected(Food food) {
50        PrefState prefs = new PrefState(getContext());
51        int currentState = prefs.getFoodState();
52        if (currentState == 0 && food.getType() != 0) {
53            NekoService.registerJob(getContext(), food.getInterval(getContext()));
54        }
55        MetricsLogger.histogram(getContext(), "egg_neko_offered_food", food.getType());
56        prefs.setFoodState(food.getType());
57        dismiss();
58    }
59
60    private class Adapter extends RecyclerView.Adapter<Holder> {
61
62        private final Context mContext;
63        private final ArrayList<Food> mFoods = new ArrayList<>();
64
65        public Adapter(Context context) {
66            mContext = context;
67            int[] foods = context.getResources().getIntArray(R.array.food_names);
68            // skip food 0, you can't choose it
69            for (int i=1; i<foods.length; i++) {
70                mFoods.add(new Food(i));
71            }
72        }
73
74        @Override
75        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
76            return new Holder(LayoutInflater.from(parent.getContext())
77                    .inflate(R.layout.food_layout, parent, false));
78        }
79
80        @Override
81        public void onBindViewHolder(final Holder holder, int position) {
82            final Food food = mFoods.get(position);
83            ((ImageView) holder.itemView.findViewById(R.id.icon))
84                    .setImageIcon(food.getIcon(mContext));
85            ((TextView) holder.itemView.findViewById(R.id.text))
86                    .setText(food.getName(mContext));
87            holder.itemView.setOnClickListener(new View.OnClickListener() {
88                @Override
89                public void onClick(View v) {
90                    onFoodSelected(mFoods.get(holder.getAdapterPosition()));
91                }
92            });
93        }
94
95        @Override
96        public int getItemCount() {
97            return mFoods.size();
98        }
99    }
100
101    public static class Holder extends RecyclerView.ViewHolder {
102
103        public Holder(View itemView) {
104            super(itemView);
105        }
106    }
107}
108