2 * Copyright (C) 2016 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.android.egg.neko;
17 import android.support.annotation.NonNull;
18 import android.app.Dialog;
19 import android.content.Context;
20 import android.support.v7.widget.GridLayoutManager;
21 import android.support.v7.widget.RecyclerView;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageView;
26 import android.widget.TextView;
28 import com.android.egg.R;
29 import com.android.internal.logging.MetricsLogger;
31 import java.util.ArrayList;
33 public class NekoDialog extends Dialog {
35 private final Adapter mAdapter;
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);
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()));
55 MetricsLogger.histogram(getContext(), "egg_neko_offered_food", food.getType());
56 prefs.setFoodState(food.getType());
60 private class Adapter extends RecyclerView.Adapter<Holder> {
62 private final Context mContext;
63 private final ArrayList<Food> mFoods = new ArrayList<>();
65 public Adapter(Context 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));
75 public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
76 return new Holder(LayoutInflater.from(parent.getContext())
77 .inflate(R.layout.food_layout, parent, false));
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() {
89 public void onClick(View v) {
90 onFoodSelected(mFoods.get(holder.getAdapterPosition()));
96 public int getItemCount() {
101 public static class Holder extends RecyclerView.ViewHolder {
103 public Holder(View itemView) {