Home | History | Annotate | Download | only in daydream
      1 /*
      2  * Copyright (C) 2014 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.tv.settings.device.display.daydream;
     18 
     19 import static android.provider.Settings.Secure.SCREENSAVER_ENABLED;
     20 
     21 import com.android.tv.settings.R;
     22 import com.android.tv.settings.dialog.old.Action;
     23 
     24 import android.content.ComponentName;
     25 import android.content.ContentResolver;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.pm.PackageManager;
     29 import android.content.pm.ResolveInfo;
     30 import android.content.res.Resources;
     31 import android.os.RemoteException;
     32 import android.os.ServiceManager;
     33 import android.provider.Settings;
     34 import android.service.dreams.DreamService;
     35 import android.service.dreams.IDreamManager;
     36 import android.util.Log;
     37 
     38 import java.util.ArrayList;
     39 import java.util.Collections;
     40 import java.util.List;
     41 
     42 /**
     43  * Manages communication with the dream manager service.
     44  */
     45 class DreamBackend {
     46 
     47     private static final String TAG = "DreamBackend";
     48     private static final boolean DEBUG = false;
     49 
     50     private final ContentResolver mContentResolver;
     51     private final PackageManager mPackageManager;
     52     private final Resources mResources;
     53     private final IDreamManager mDreamManager;
     54     private final boolean mDreamsEnabledByDefault;
     55     private final ArrayList<DreamInfoAction> mDreamInfoActions;
     56     private String mActiveDreamTitle;
     57 
     58     DreamBackend(Context context) {
     59         mContentResolver = context.getContentResolver();
     60         mPackageManager = context.getPackageManager();
     61         mResources = context.getResources();
     62         mDreamManager = IDreamManager.Stub.asInterface(
     63                 ServiceManager.getService(DreamService.DREAM_SERVICE));
     64         mDreamsEnabledByDefault = mResources.getBoolean(
     65                 com.android.internal.R.bool.config_dreamsEnabledByDefault);
     66         mDreamInfoActions = new ArrayList<DreamInfoAction>();
     67     }
     68 
     69     void initDreamInfoActions() {
     70         ComponentName activeDream = getActiveDream();
     71         List<ResolveInfo> resolveInfos = mPackageManager.queryIntentServices(
     72                 new Intent(DreamService.SERVICE_INTERFACE), PackageManager.GET_META_DATA);
     73         for (int i = 0, size = resolveInfos.size(); i< size; i++) {
     74             ResolveInfo resolveInfo = resolveInfos.get(i);
     75             if (resolveInfo.serviceInfo == null) {
     76                 continue;
     77             }
     78             DreamInfoAction action = new DreamInfoAction(resolveInfo,
     79                     isEnabled() ? activeDream : null, mPackageManager);
     80             mDreamInfoActions.add(action);
     81             if(action.isChecked() && isEnabled()) {
     82                 mActiveDreamTitle = action.getTitle();
     83             }
     84         }
     85         Collections.sort(mDreamInfoActions,
     86                 new DreamInfoAction.DreamInfoActionComparator(getDefaultDream()));
     87         DreamInfoAction none = new NoneDreamInfoAction(
     88                 mResources.getString(R.string.device_daydreams_none), isEnabled());
     89         mDreamInfoActions.add(0, none);
     90         if(mActiveDreamTitle == null) {
     91             mActiveDreamTitle = none.getTitle();
     92         }
     93     }
     94 
     95     ArrayList<Action> getDreamInfoActions() {
     96         ArrayList<Action> actions = new ArrayList<Action>();
     97         actions.addAll(mDreamInfoActions);
     98         return actions;
     99     }
    100 
    101     boolean isEnabled() {
    102         int enableDefault = mDreamsEnabledByDefault ? 1 : 0;
    103         return Settings.Secure.getInt(mContentResolver, SCREENSAVER_ENABLED, enableDefault) == 1;
    104     }
    105 
    106     void setEnabled(boolean value) {
    107         Settings.Secure.putInt(mContentResolver, SCREENSAVER_ENABLED, value ? 1 : 0);
    108     }
    109 
    110     void setActiveDream(ComponentName dream) {
    111         if (mDreamManager != null) {
    112             try {
    113                 ComponentName[] dreams = dream == null ? null : new ComponentName[] { dream };
    114                 mDreamManager.setDreamComponents(dreams);
    115             } catch (RemoteException e) {
    116                 Log.w(TAG, "Failed to set active dream to " + dream, e);
    117             }
    118         }
    119     }
    120 
    121     void setActiveDreamInfoAction(DreamInfoAction dreamInfoAction) {
    122         mActiveDreamTitle = dreamInfoAction.getTitle();
    123     }
    124 
    125     String getActiveDreamTitle() {
    126         return mActiveDreamTitle;
    127     }
    128 
    129     ComponentName getActiveDream() {
    130         if (mDreamManager != null) {
    131             try {
    132                 ComponentName[] dreams = mDreamManager.getDreamComponents();
    133                 return dreams != null && dreams.length > 0 ? dreams[0] : null;
    134             } catch (RemoteException e) {
    135                 Log.w(TAG, "Failed to get active dream", e);
    136             }
    137         }
    138         return null;
    139     }
    140 
    141     void startDreaming() {
    142         if (mDreamManager != null) {
    143             try {
    144                 mDreamManager.dream();
    145             } catch (RemoteException e) {
    146                 Log.w(TAG, "Failed to dream", e);
    147             }
    148         }
    149     }
    150 
    151     private ComponentName getDefaultDream() {
    152         if (mDreamManager != null) {
    153             try {
    154                 return mDreamManager.getDefaultDreamComponent();
    155             } catch (RemoteException e) {
    156                 Log.w(TAG, "Failed to get default dream", e);
    157             }
    158         }
    159         return null;
    160     }
    161 }
    162