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