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