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         if (foodState != 0) {
     72             NekoService.registerJobIfNeeded(this, food.getInterval(this));
     73         }
     74         tile.setIcon(food.getIcon(this));
     75         tile.setLabel(food.getName(this));
     76         tile.setState(foodState != 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
     77         tile.updateTile();
     78     }
     79 
     80     @Override
     81     public void onClick() {
     82         if (mPrefs.getFoodState() != 0) {
     83             // there's already food loaded, let's empty it
     84             MetricsLogger.count(this, "egg_neko_empty_food", 1);
     85             mPrefs.setFoodState(0);
     86             NekoService.cancelJob(this);
     87         } else {
     88             // time to feed the cats
     89             if (isLocked()) {
     90                 if (isSecure()) {
     91                     Log.d(TAG, "startActivityAndCollapse");
     92                     Intent intent = new Intent(this, NekoLockedActivity.class);
     93                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     94                     startActivityAndCollapse(intent);
     95                 } else {
     96                     unlockAndRun(new Runnable() {
     97                         @Override
     98                         public void run() {
     99                             showNekoDialog();
    100                         }
    101                     });
    102                 }
    103             } else {
    104                 showNekoDialog();
    105             }
    106         }
    107     }
    108 
    109     private void showNekoDialog() {
    110         Log.d(TAG, "showNekoDialog");
    111         MetricsLogger.count(this, "egg_neko_select_food", 1);
    112         showDialog(new NekoDialog(this));
    113     }
    114 }
    115