NekoTile.java revision 27a9fcc61823f919cee773df563b49ee11004f3b
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.content.Intent;
18import android.service.quicksettings.Tile;
19import android.service.quicksettings.TileService;
20import android.util.Log;
21
22import com.android.egg.neko.PrefState.PrefsListener;
23
24public class NekoTile extends TileService implements PrefsListener {
25
26    private static final String TAG = "NekoTile";
27
28    private PrefState mPrefs;
29
30    @Override
31    public void onCreate() {
32        super.onCreate();
33        mPrefs = new PrefState(this);
34    }
35
36    @Override
37    public void onStartListening() {
38        super.onStartListening();
39        mPrefs.setListener(this);
40        updateState();
41    }
42
43    @Override
44    public void onStopListening() {
45        super.onStopListening();
46        mPrefs.setListener(null);
47    }
48
49    @Override
50    public void onPrefsChanged() {
51        updateState();
52    }
53
54    private void updateState() {
55        Tile tile = getQsTile();
56        int foodState = mPrefs.getFoodState();
57        Food food = new Food(foodState);
58        tile.setIcon(food.getIcon(this));
59        tile.setLabel(food.getName(this));
60        tile.setState(foodState != 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
61        tile.updateTile();
62    }
63
64    @Override
65    public void onClick() {
66        if (mPrefs.getFoodState() != 0) {
67            // there's already food loaded, let's empty it
68            mPrefs.setFoodState(0);
69            NekoService.cancelJob(this);
70        } else {
71            // time to feed the cats
72            if (isLocked()) {
73                if (isSecure()) {
74                    Log.d(TAG, "startActivityAndCollapse");
75                    Intent intent = new Intent(this, NekoLockedActivity.class);
76                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
77                    startActivityAndCollapse(intent);
78                } else {
79                    unlockAndRun(new Runnable() {
80                        @Override
81                        public void run() {
82                            showNekoDialog();
83                        }
84                    });
85                }
86            } else {
87                showNekoDialog();
88            }
89        }
90    }
91
92    private void showNekoDialog() {
93        Log.d(TAG, "showNekoDialog");
94        showDialog(new NekoDialog(this));
95    }
96}
97