Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 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.server.tv; |
| 18 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 19 | import static android.media.tv.TvInputManager.INPUT_STATE_CONNECTED; |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 20 | import static android.media.tv.TvInputManager.INPUT_STATE_CONNECTED_STANDBY; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 21 | |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ComponentName; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
| 27 | import android.content.ContentUris; |
| 28 | import android.content.ContentValues; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.content.ServiceConnection; |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 33 | import android.content.pm.ActivityInfo; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager.NameNotFoundException; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 36 | import android.content.pm.ResolveInfo; |
| 37 | import android.content.pm.ServiceInfo; |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 38 | import android.graphics.Rect; |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 39 | import android.hardware.hdmi.HdmiControlManager; |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 40 | import android.hardware.hdmi.HdmiDeviceInfo; |
Jae Seo | 4b34cc7 | 2015-05-15 17:29:39 -0700 | [diff] [blame] | 41 | import android.media.PlaybackParams; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 42 | import android.media.tv.DvbDeviceInfo; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 43 | import android.media.tv.ITvInputClient; |
| 44 | import android.media.tv.ITvInputHardware; |
| 45 | import android.media.tv.ITvInputHardwareCallback; |
| 46 | import android.media.tv.ITvInputManager; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 47 | import android.media.tv.ITvInputManagerCallback; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 48 | import android.media.tv.ITvInputService; |
| 49 | import android.media.tv.ITvInputServiceCallback; |
| 50 | import android.media.tv.ITvInputSession; |
| 51 | import android.media.tv.ITvInputSessionCallback; |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 52 | import android.media.tv.TvContentRating; |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 53 | import android.media.tv.TvContentRatingSystemInfo; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 54 | import android.media.tv.TvContract; |
| 55 | import android.media.tv.TvInputHardwareInfo; |
| 56 | import android.media.tv.TvInputInfo; |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 57 | import android.media.tv.TvInputManager; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 58 | import android.media.tv.TvInputService; |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 59 | import android.media.tv.TvStreamConfig; |
Dongwon Kang | 1f21391 | 2014-07-02 18:35:08 +0900 | [diff] [blame] | 60 | import android.media.tv.TvTrackInfo; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 61 | import android.net.Uri; |
| 62 | import android.os.Binder; |
Youngsang Cho | 832860f | 2014-05-21 20:54:03 +0900 | [diff] [blame] | 63 | import android.os.Bundle; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 64 | import android.os.Handler; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 65 | import android.os.IBinder; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 66 | import android.os.Looper; |
| 67 | import android.os.Message; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 68 | import android.os.ParcelFileDescriptor; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 69 | import android.os.Process; |
| 70 | import android.os.RemoteException; |
| 71 | import android.os.UserHandle; |
Jae Seo | c2a8951 | 2016-01-28 10:38:11 -0800 | [diff] [blame] | 72 | import android.text.TextUtils; |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 73 | import android.util.Slog; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 74 | import android.util.SparseArray; |
Jae Seo | 6a6059a | 2014-04-17 21:35:29 -0700 | [diff] [blame] | 75 | import android.view.InputChannel; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 76 | import android.view.Surface; |
| 77 | |
| 78 | import com.android.internal.content.PackageMonitor; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 79 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 80 | import com.android.internal.util.DumpUtils; |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 81 | import com.android.internal.util.IndentingPrintWriter; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 82 | import com.android.server.IoThread; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 83 | import com.android.server.SystemService; |
| 84 | |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 85 | import java.io.File; |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 86 | import java.io.FileDescriptor; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 87 | import java.io.FileNotFoundException; |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 88 | import java.io.PrintWriter; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 89 | import java.util.ArrayList; |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 90 | import java.util.Arrays; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 91 | import java.util.Collections; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 92 | import java.util.HashMap; |
Jae Seo | 5c80ad2 | 2014-06-12 19:52:58 -0700 | [diff] [blame] | 93 | import java.util.HashSet; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 94 | import java.util.Iterator; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 95 | import java.util.List; |
| 96 | import java.util.Map; |
Jae Seo | 5c80ad2 | 2014-06-12 19:52:58 -0700 | [diff] [blame] | 97 | import java.util.Set; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 98 | import java.util.regex.Matcher; |
| 99 | import java.util.regex.Pattern; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 100 | |
| 101 | /** This class provides a system service that manages television inputs. */ |
| 102 | public final class TvInputManagerService extends SystemService { |
Jae Seo | ee2ec05 | 2014-09-14 10:30:05 -0700 | [diff] [blame] | 103 | private static final boolean DEBUG = false; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 104 | private static final String TAG = "TvInputManagerService"; |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 105 | private static final String DVB_DIRECTORY = "/dev/dvb"; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 106 | |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 107 | // There are two different formats of DVB frontend devices. One is /dev/dvb%d.frontend%d, |
| 108 | // another one is /dev/dvb/adapter%d/frontend%d. Followings are the patterns for selecting the |
| 109 | // DVB frontend devices from the list of files in the /dev and /dev/dvb/adapter%d directory. |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 110 | private static final Pattern sFrontEndDevicePattern = |
| 111 | Pattern.compile("^dvb([0-9]+)\\.frontend([0-9]+)$"); |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 112 | private static final Pattern sAdapterDirPattern = |
| 113 | Pattern.compile("^adapter([0-9]+)$"); |
| 114 | private static final Pattern sFrontEndInAdapterDirPattern = |
| 115 | Pattern.compile("^frontend([0-9]+)$"); |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 116 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 117 | private final Context mContext; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 118 | private final TvInputHardwareManager mTvInputHardwareManager; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 119 | |
| 120 | // A global lock. |
| 121 | private final Object mLock = new Object(); |
| 122 | |
| 123 | // ID of the current user. |
Xiaohui Chen | 233d94c | 2015-07-30 15:08:00 -0700 | [diff] [blame] | 124 | private int mCurrentUserId = UserHandle.USER_SYSTEM; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 125 | |
| 126 | // A map from user id to UserState. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 127 | private final SparseArray<UserState> mUserStates = new SparseArray<>(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 128 | |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 129 | private final WatchLogHandler mWatchLogHandler; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 130 | |
Kyeongkab.Nam | 8d6b71a | 2019-01-16 10:44:37 +0900 | [diff] [blame] | 131 | private IBinder.DeathRecipient mDeathRecipient; |
| 132 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 133 | public TvInputManagerService(Context context) { |
| 134 | super(context); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 135 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 136 | mContext = context; |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 137 | mWatchLogHandler = new WatchLogHandler(mContext.getContentResolver(), |
| 138 | IoThread.get().getLooper()); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 139 | mTvInputHardwareManager = new TvInputHardwareManager(context, new HardwareListener()); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 140 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 141 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 142 | getOrCreateUserStateLocked(mCurrentUserId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onStart() { |
| 148 | publishBinderService(Context.TV_INPUT_SERVICE, new BinderService()); |
| 149 | } |
| 150 | |
Ji-Hwan Lee | 0ceb7e4 | 2014-06-21 04:42:05 +0900 | [diff] [blame] | 151 | @Override |
| 152 | public void onBootPhase(int phase) { |
| 153 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 154 | registerBroadcastReceivers(); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 155 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
Ji-Hwan Lee | 0ceb7e4 | 2014-06-21 04:42:05 +0900 | [diff] [blame] | 156 | synchronized (mLock) { |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 157 | buildTvInputListLocked(mCurrentUserId, null); |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 158 | buildTvContentRatingSystemListLocked(mCurrentUserId); |
Ji-Hwan Lee | 0ceb7e4 | 2014-06-21 04:42:05 +0900 | [diff] [blame] | 159 | } |
| 160 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 161 | mTvInputHardwareManager.onBootPhase(phase); |
Ji-Hwan Lee | 0ceb7e4 | 2014-06-21 04:42:05 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Youngsang Cho | 19c47d7 | 2016-05-05 09:21:24 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public void onUnlockUser(int userHandle) { |
| 166 | if (DEBUG) Slog.d(TAG, "onUnlockUser(userHandle=" + userHandle + ")"); |
| 167 | synchronized (mLock) { |
| 168 | if (mCurrentUserId != userHandle) { |
| 169 | return; |
| 170 | } |
| 171 | buildTvInputListLocked(mCurrentUserId, null); |
| 172 | buildTvContentRatingSystemListLocked(mCurrentUserId); |
| 173 | } |
| 174 | } |
| 175 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 176 | private void registerBroadcastReceivers() { |
| 177 | PackageMonitor monitor = new PackageMonitor() { |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 178 | private void buildTvInputList(String[] packages) { |
| 179 | synchronized (mLock) { |
Dongwon Kang | a897e5f | 2016-04-19 13:39:47 -0700 | [diff] [blame] | 180 | if (mCurrentUserId == getChangingUserId()) { |
| 181 | buildTvInputListLocked(mCurrentUserId, packages); |
| 182 | buildTvContentRatingSystemListLocked(mCurrentUserId); |
| 183 | } |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 189 | if (DEBUG) Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")"); |
| 190 | // This callback is invoked when the TV input is reinstalled. |
| 191 | // In this case, isReplacing() always returns true. |
| 192 | buildTvInputList(new String[] { packageName }); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onPackagesAvailable(String[] packages) { |
| 197 | if (DEBUG) { |
| 198 | Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")"); |
| 199 | } |
| 200 | // This callback is invoked when the media on which some packages exist become |
| 201 | // available. |
| 202 | if (isReplacing()) { |
| 203 | buildTvInputList(packages); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void onPackagesUnavailable(String[] packages) { |
| 209 | // This callback is invoked when the media on which some packages exist become |
| 210 | // unavailable. |
| 211 | if (DEBUG) { |
| 212 | Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) |
| 213 | + ")"); |
| 214 | } |
| 215 | if (isReplacing()) { |
| 216 | buildTvInputList(packages); |
| 217 | } |
| 218 | } |
| 219 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 220 | @Override |
| 221 | public void onSomePackagesChanged() { |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 222 | // TODO: Use finer-grained methods(e.g. onPackageAdded, onPackageRemoved) to manage |
| 223 | // the TV inputs. |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 224 | if (DEBUG) Slog.d(TAG, "onSomePackagesChanged()"); |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 225 | if (isReplacing()) { |
| 226 | if (DEBUG) Slog.d(TAG, "Skipped building TV input list due to replacing"); |
| 227 | // When the package is updated, buildTvInputListLocked is called in other |
| 228 | // methods instead. |
| 229 | return; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 230 | } |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 231 | buildTvInputList(null); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 232 | } |
Jae Seo | 5c80ad2 | 2014-06-12 19:52:58 -0700 | [diff] [blame] | 233 | |
| 234 | @Override |
Dongwon Kang | 31a8f84 | 2015-04-08 18:26:23 -0700 | [diff] [blame] | 235 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 236 | // The input list needs to be updated in any cases, regardless of whether |
| 237 | // it happened to the whole package or a specific component. Returning true so that |
| 238 | // the update can be handled in {@link #onSomePackagesChanged}. |
| 239 | return true; |
| 240 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 241 | }; |
| 242 | monitor.register(mContext, null, UserHandle.ALL, true); |
| 243 | |
| 244 | IntentFilter intentFilter = new IntentFilter(); |
| 245 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
| 246 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 247 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 248 | @Override |
| 249 | public void onReceive(Context context, Intent intent) { |
| 250 | String action = intent.getAction(); |
| 251 | if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 252 | switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
| 253 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 254 | removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
| 255 | } |
| 256 | } |
| 257 | }, UserHandle.ALL, intentFilter, null, null); |
| 258 | } |
| 259 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 260 | private static boolean hasHardwarePermission(PackageManager pm, ComponentName component) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 261 | return pm.checkPermission(android.Manifest.permission.TV_INPUT_HARDWARE, |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 262 | component.getPackageName()) == PackageManager.PERMISSION_GRANTED; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 265 | private void buildTvInputListLocked(int userId, String[] updatedPackages) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 266 | UserState userState = getOrCreateUserStateLocked(userId); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 267 | userState.packageSet.clear(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 268 | |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 269 | if (DEBUG) Slog.d(TAG, "buildTvInputList"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 270 | PackageManager pm = mContext.getPackageManager(); |
Jae Seo | 76976fa | 2015-05-20 21:51:16 -0700 | [diff] [blame] | 271 | List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
Chulwoo Lee | e7bb7d6 | 2014-05-27 14:10:37 +0900 | [diff] [blame] | 272 | new Intent(TvInputService.SERVICE_INTERFACE), |
Jae Seo | 76976fa | 2015-05-20 21:51:16 -0700 | [diff] [blame] | 273 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA, |
| 274 | userId); |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 275 | List<TvInputInfo> inputList = new ArrayList<>(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 276 | for (ResolveInfo ri : services) { |
| 277 | ServiceInfo si = ri.serviceInfo; |
| 278 | if (!android.Manifest.permission.BIND_TV_INPUT.equals(si.permission)) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 279 | Slog.w(TAG, "Skipping TV input " + si.name + ": it does not require the permission " |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 280 | + android.Manifest.permission.BIND_TV_INPUT); |
| 281 | continue; |
| 282 | } |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 283 | |
| 284 | ComponentName component = new ComponentName(si.packageName, si.name); |
| 285 | if (hasHardwarePermission(pm, component)) { |
| 286 | ServiceState serviceState = userState.serviceStateMap.get(component); |
| 287 | if (serviceState == null) { |
Jae Seo | c980f43d | 2016-02-09 23:46:58 -0800 | [diff] [blame] | 288 | // New hardware input found. Create a new ServiceState and connect to the |
| 289 | // service to populate the hardware list. |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 290 | serviceState = new ServiceState(component, userId); |
| 291 | userState.serviceStateMap.put(component, serviceState); |
Wonsik Kim | f271eac | 2014-08-30 12:55:10 +0900 | [diff] [blame] | 292 | updateServiceConnectionLocked(component, userId); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 293 | } else { |
Shubang | 71d5c76 | 2017-02-23 15:46:40 -0800 | [diff] [blame] | 294 | inputList.addAll(serviceState.hardwareInputMap.values()); |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 295 | } |
| 296 | } else { |
| 297 | try { |
Jae Seo | c03671f | 2016-01-26 15:15:22 -0800 | [diff] [blame] | 298 | TvInputInfo info = new TvInputInfo.Builder(mContext, ri).build(); |
| 299 | inputList.add(info); |
Jae Seo | 18c0cfb | 2016-05-16 18:19:11 -0700 | [diff] [blame] | 300 | } catch (Exception e) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 301 | Slog.e(TAG, "failed to load TV input " + si.name, e); |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 302 | continue; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 303 | } |
Chulwoo Lee | e7bb7d6 | 2014-05-27 14:10:37 +0900 | [diff] [blame] | 304 | } |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 305 | userState.packageSet.add(si.packageName); |
| 306 | } |
| 307 | |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 308 | Map<String, TvInputState> inputMap = new HashMap<>(); |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 309 | for (TvInputInfo info : inputList) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 310 | if (DEBUG) { |
| 311 | Slog.d(TAG, "add " + info.getId()); |
| 312 | } |
Jae Seo | abda420 | 2016-01-28 19:13:04 -0800 | [diff] [blame] | 313 | TvInputState inputState = userState.inputMap.get(info.getId()); |
| 314 | if (inputState == null) { |
| 315 | inputState = new TvInputState(); |
Jae Seo | 9cc28e5 | 2014-08-12 16:45:58 -0700 | [diff] [blame] | 316 | } |
Shubang | 2263fb0 | 2016-05-13 16:51:53 -0700 | [diff] [blame] | 317 | inputState.info = info; |
Jae Seo | abda420 | 2016-01-28 19:13:04 -0800 | [diff] [blame] | 318 | inputMap.put(info.getId(), inputState); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 319 | } |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 320 | |
| 321 | for (String inputId : inputMap.keySet()) { |
| 322 | if (!userState.inputMap.containsKey(inputId)) { |
| 323 | notifyInputAddedLocked(userState, inputId); |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 324 | } else if (updatedPackages != null) { |
| 325 | // Notify the package updates |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 326 | ComponentName component = inputMap.get(inputId).info.getComponent(); |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 327 | for (String updatedPackage : updatedPackages) { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 328 | if (component.getPackageName().equals(updatedPackage)) { |
| 329 | updateServiceConnectionLocked(component, userId); |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 330 | notifyInputUpdatedLocked(userState, inputId); |
| 331 | break; |
| 332 | } |
| 333 | } |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | for (String inputId : userState.inputMap.keySet()) { |
| 338 | if (!inputMap.containsKey(inputId)) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 339 | TvInputInfo info = userState.inputMap.get(inputId).info; |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 340 | ServiceState serviceState = userState.serviceStateMap.get(info.getComponent()); |
| 341 | if (serviceState != null) { |
| 342 | abortPendingCreateSessionRequestsLocked(serviceState, inputId, userId); |
| 343 | } |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 344 | notifyInputRemovedLocked(userState, inputId); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | userState.inputMap.clear(); |
| 349 | userState.inputMap = inputMap; |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 350 | } |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 351 | |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 352 | private void buildTvContentRatingSystemListLocked(int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 353 | UserState userState = getOrCreateUserStateLocked(userId); |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 354 | userState.contentRatingSystemList.clear(); |
| 355 | |
| 356 | final PackageManager pm = mContext.getPackageManager(); |
| 357 | Intent intent = new Intent(TvInputManager.ACTION_QUERY_CONTENT_RATING_SYSTEMS); |
| 358 | for (ResolveInfo resolveInfo : |
| 359 | pm.queryBroadcastReceivers(intent, PackageManager.GET_META_DATA)) { |
| 360 | ActivityInfo receiver = resolveInfo.activityInfo; |
| 361 | Bundle metaData = receiver.metaData; |
| 362 | if (metaData == null) { |
| 363 | continue; |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 364 | } |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 365 | |
| 366 | int xmlResId = metaData.getInt(TvInputManager.META_DATA_CONTENT_RATING_SYSTEMS); |
| 367 | if (xmlResId == 0) { |
| 368 | Slog.w(TAG, "Missing meta-data '" |
| 369 | + TvInputManager.META_DATA_CONTENT_RATING_SYSTEMS + "' on receiver " |
| 370 | + receiver.packageName + "/" + receiver.name); |
| 371 | continue; |
| 372 | } |
| 373 | userState.contentRatingSystemList.add( |
| 374 | TvContentRatingSystemInfo.createTvContentRatingSystemInfo(xmlResId, |
| 375 | receiver.applicationInfo)); |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 376 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | private void switchUser(int userId) { |
| 380 | synchronized (mLock) { |
| 381 | if (mCurrentUserId == userId) { |
| 382 | return; |
| 383 | } |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 384 | UserState userState = mUserStates.get(mCurrentUserId); |
| 385 | List<SessionState> sessionStatesToRelease = new ArrayList<>(); |
| 386 | for (SessionState sessionState : userState.sessionStateMap.values()) { |
| 387 | if (sessionState.session != null && !sessionState.isRecordingSession) { |
| 388 | sessionStatesToRelease.add(sessionState); |
| 389 | } |
| 390 | } |
| 391 | for (SessionState sessionState : sessionStatesToRelease) { |
| 392 | try { |
| 393 | sessionState.session.release(); |
| 394 | } catch (RemoteException e) { |
| 395 | Slog.e(TAG, "error in release", e); |
| 396 | } |
| 397 | clearSessionAndNotifyClientLocked(sessionState); |
| 398 | } |
| 399 | |
| 400 | for (Iterator<ComponentName> it = userState.serviceStateMap.keySet().iterator(); |
| 401 | it.hasNext(); ) { |
| 402 | ComponentName component = it.next(); |
| 403 | ServiceState serviceState = userState.serviceStateMap.get(component); |
| 404 | if (serviceState != null && serviceState.sessionTokens.isEmpty()) { |
| 405 | if (serviceState.callback != null) { |
| 406 | try { |
| 407 | serviceState.service.unregisterCallback(serviceState.callback); |
| 408 | } catch (RemoteException e) { |
| 409 | Slog.e(TAG, "error in unregisterCallback", e); |
| 410 | } |
| 411 | } |
| 412 | mContext.unbindService(serviceState.connection); |
| 413 | it.remove(); |
| 414 | } |
| 415 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 416 | |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 417 | mCurrentUserId = userId; |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 418 | getOrCreateUserStateLocked(userId); |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 419 | buildTvInputListLocked(userId, null); |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 420 | buildTvContentRatingSystemListLocked(userId); |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 421 | mWatchLogHandler.obtainMessage(WatchLogHandler.MSG_SWITCH_CONTENT_RESOLVER, |
| 422 | getContentResolverForUser(userId)).sendToTarget(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 426 | private void clearSessionAndNotifyClientLocked(SessionState state) { |
| 427 | if (state.client != null) { |
| 428 | try { |
| 429 | state.client.onSessionReleased(state.seq); |
| 430 | } catch(RemoteException e) { |
| 431 | Slog.e(TAG, "error in onSessionReleased", e); |
| 432 | } |
| 433 | } |
| 434 | // If there are any other sessions based on this session, they should be released. |
| 435 | UserState userState = getOrCreateUserStateLocked(state.userId); |
| 436 | for (SessionState sessionState : userState.sessionStateMap.values()) { |
| 437 | if (state.sessionToken == sessionState.hardwareSessionToken) { |
| 438 | releaseSessionLocked(sessionState.sessionToken, Process.SYSTEM_UID, state.userId); |
| 439 | try { |
| 440 | sessionState.client.onSessionReleased(sessionState.seq); |
| 441 | } catch (RemoteException e) { |
| 442 | Slog.e(TAG, "error in onSessionReleased", e); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | removeSessionStateLocked(state.sessionToken, state.userId); |
| 447 | } |
| 448 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 449 | private void removeUser(int userId) { |
| 450 | synchronized (mLock) { |
Jae Seo | b06cb88 | 2014-04-09 12:08:17 -0700 | [diff] [blame] | 451 | UserState userState = mUserStates.get(userId); |
| 452 | if (userState == null) { |
| 453 | return; |
| 454 | } |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 455 | // Release all created sessions. |
| 456 | for (SessionState state : userState.sessionStateMap.values()) { |
| 457 | if (state.session != null) { |
| 458 | try { |
| 459 | state.session.release(); |
| 460 | } catch (RemoteException e) { |
| 461 | Slog.e(TAG, "error in release", e); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | userState.sessionStateMap.clear(); |
| 466 | |
| 467 | // Unregister all callbacks and unbind all services. |
| 468 | for (ServiceState serviceState : userState.serviceStateMap.values()) { |
| 469 | if (serviceState.service != null) { |
| 470 | if (serviceState.callback != null) { |
| 471 | try { |
| 472 | serviceState.service.unregisterCallback(serviceState.callback); |
| 473 | } catch (RemoteException e) { |
| 474 | Slog.e(TAG, "error in unregisterCallback", e); |
| 475 | } |
| 476 | } |
| 477 | mContext.unbindService(serviceState.connection); |
| 478 | } |
| 479 | } |
| 480 | userState.serviceStateMap.clear(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 481 | |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 482 | // Clear everything else. |
| 483 | userState.inputMap.clear(); |
| 484 | userState.packageSet.clear(); |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 485 | userState.contentRatingSystemList.clear(); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 486 | userState.clientStateMap.clear(); |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 487 | userState.callbackSet.clear(); |
| 488 | userState.mainSessionToken = null; |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 489 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 490 | mUserStates.remove(userId); |
| 491 | } |
| 492 | } |
| 493 | |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 494 | private ContentResolver getContentResolverForUser(int userId) { |
| 495 | UserHandle user = new UserHandle(userId); |
| 496 | Context context; |
| 497 | try { |
| 498 | context = mContext.createPackageContextAsUser("android", 0, user); |
| 499 | } catch (NameNotFoundException e) { |
Jae Seo | 2a2b299 | 2016-01-12 23:13:14 -0800 | [diff] [blame] | 500 | Slog.e(TAG, "failed to create package context as user " + user); |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 501 | context = mContext; |
| 502 | } |
| 503 | return context.getContentResolver(); |
| 504 | } |
| 505 | |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 506 | private UserState getOrCreateUserStateLocked(int userId) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 507 | UserState userState = mUserStates.get(userId); |
| 508 | if (userState == null) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 509 | userState = new UserState(mContext, userId); |
| 510 | mUserStates.put(userId, userState); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 511 | } |
| 512 | return userState; |
| 513 | } |
| 514 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 515 | private ServiceState getServiceStateLocked(ComponentName component, int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 516 | UserState userState = getOrCreateUserStateLocked(userId); |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 517 | ServiceState serviceState = userState.serviceStateMap.get(component); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 518 | if (serviceState == null) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 519 | throw new IllegalStateException("Service state not found for " + component + " (userId=" |
Sungsoo Lim | 7de5e23 | 2014-04-12 16:51:27 +0900 | [diff] [blame] | 520 | + userId + ")"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 521 | } |
| 522 | return serviceState; |
| 523 | } |
| 524 | |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 525 | private SessionState getSessionStateLocked(IBinder sessionToken, int callingUid, int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 526 | UserState userState = getOrCreateUserStateLocked(userId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 527 | SessionState sessionState = userState.sessionStateMap.get(sessionToken); |
| 528 | if (sessionState == null) { |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 529 | throw new SessionNotFoundException("Session state not found for token " + sessionToken); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 530 | } |
| 531 | // Only the application that requested this session or the system can access it. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 532 | if (callingUid != Process.SYSTEM_UID && callingUid != sessionState.callingUid) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 533 | throw new SecurityException("Illegal access to the session with token " + sessionToken |
| 534 | + " from uid " + callingUid); |
| 535 | } |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 536 | return sessionState; |
| 537 | } |
| 538 | |
| 539 | private ITvInputSession getSessionLocked(IBinder sessionToken, int callingUid, int userId) { |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 540 | return getSessionLocked(getSessionStateLocked(sessionToken, callingUid, userId)); |
| 541 | } |
| 542 | |
| 543 | private ITvInputSession getSessionLocked(SessionState sessionState) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 544 | ITvInputSession session = sessionState.session; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 545 | if (session == null) { |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 546 | throw new IllegalStateException("Session not yet created for token " |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 547 | + sessionState.sessionToken); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 548 | } |
| 549 | return session; |
| 550 | } |
| 551 | |
| 552 | private int resolveCallingUserId(int callingPid, int callingUid, int requestedUserId, |
| 553 | String methodName) { |
| 554 | return ActivityManager.handleIncomingUser(callingPid, callingUid, requestedUserId, false, |
| 555 | false, methodName, null); |
| 556 | } |
| 557 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 558 | private void updateServiceConnectionLocked(ComponentName component, int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 559 | UserState userState = getOrCreateUserStateLocked(userId); |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 560 | ServiceState serviceState = userState.serviceStateMap.get(component); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 561 | if (serviceState == null) { |
| 562 | return; |
| 563 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 564 | if (serviceState.reconnecting) { |
| 565 | if (!serviceState.sessionTokens.isEmpty()) { |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 566 | // wait until all the sessions are removed. |
| 567 | return; |
| 568 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 569 | serviceState.reconnecting = false; |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 570 | } |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 571 | |
| 572 | boolean shouldBind; |
| 573 | if (userId == mCurrentUserId) { |
| 574 | shouldBind = !serviceState.sessionTokens.isEmpty() || serviceState.isHardware; |
| 575 | } else { |
| 576 | // For a non-current user, |
| 577 | // if sessionTokens is not empty, it contains recording sessions only |
| 578 | // because other sessions must have been removed while switching user |
| 579 | // and non-recording sessions are not created by createSession(). |
| 580 | shouldBind = !serviceState.sessionTokens.isEmpty(); |
| 581 | } |
| 582 | |
| 583 | if (serviceState.service == null && shouldBind) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 584 | // This means that the service is not yet connected but its state indicates that we |
| 585 | // have pending requests. Then, connect the service. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 586 | if (serviceState.bound) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 587 | // We have already bound to the service so we don't try to bind again until after we |
| 588 | // unbind later on. |
| 589 | return; |
| 590 | } |
| 591 | if (DEBUG) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 592 | Slog.d(TAG, "bindServiceAsUser(service=" + component + ", userId=" + userId + ")"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 593 | } |
Sungsoo Lim | d6672b5 | 2014-04-30 10:43:26 +0900 | [diff] [blame] | 594 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 595 | Intent i = new Intent(TvInputService.SERVICE_INTERFACE).setComponent(component); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 596 | serviceState.bound = mContext.bindServiceAsUser( |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 597 | i, serviceState.connection, |
| 598 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, |
| 599 | new UserHandle(userId)); |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 600 | } else if (serviceState.service != null && !shouldBind) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 601 | // This means that the service is already connected but its state indicates that we have |
| 602 | // nothing to do with it. Then, disconnect the service. |
| 603 | if (DEBUG) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 604 | Slog.d(TAG, "unbindService(service=" + component + ")"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 605 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 606 | mContext.unbindService(serviceState.connection); |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 607 | userState.serviceStateMap.remove(component); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 611 | private void abortPendingCreateSessionRequestsLocked(ServiceState serviceState, |
| 612 | String inputId, int userId) { |
| 613 | // Let clients know the create session requests are failed. |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 614 | UserState userState = getOrCreateUserStateLocked(userId); |
Dongwon Kang | f7f49dd | 2014-08-27 20:48:22 +0900 | [diff] [blame] | 615 | List<SessionState> sessionsToAbort = new ArrayList<>(); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 616 | for (IBinder sessionToken : serviceState.sessionTokens) { |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 617 | SessionState sessionState = userState.sessionStateMap.get(sessionToken); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 618 | if (sessionState.session == null && (inputId == null |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 619 | || sessionState.inputId.equals(inputId))) { |
Dongwon Kang | f7f49dd | 2014-08-27 20:48:22 +0900 | [diff] [blame] | 620 | sessionsToAbort.add(sessionState); |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 621 | } |
| 622 | } |
Dongwon Kang | f7f49dd | 2014-08-27 20:48:22 +0900 | [diff] [blame] | 623 | for (SessionState sessionState : sessionsToAbort) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 624 | removeSessionStateLocked(sessionState.sessionToken, sessionState.userId); |
| 625 | sendSessionTokenToClientLocked(sessionState.client, |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 626 | sessionState.inputId, null, null, sessionState.seq); |
Dongwon Kang | f7f49dd | 2014-08-27 20:48:22 +0900 | [diff] [blame] | 627 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 628 | updateServiceConnectionLocked(serviceState.component, userId); |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 629 | } |
| 630 | |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 631 | private boolean createSessionInternalLocked(ITvInputService service, IBinder sessionToken, |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 632 | int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 633 | UserState userState = getOrCreateUserStateLocked(userId); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 634 | SessionState sessionState = userState.sessionStateMap.get(sessionToken); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 635 | if (DEBUG) { |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 636 | Slog.d(TAG, "createSessionInternalLocked(inputId=" + sessionState.inputId + ")"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 637 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 638 | InputChannel[] channels = InputChannel.openInputChannelPair(sessionToken.toString()); |
Jae Seo | 6a6059a | 2014-04-17 21:35:29 -0700 | [diff] [blame] | 639 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 640 | // Set up a callback to send the session token. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 641 | ITvInputSessionCallback callback = new SessionCallback(sessionState, channels); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 642 | |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 643 | boolean created = true; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 644 | // Create a session. When failed, send a null token immediately. |
| 645 | try { |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 646 | if (sessionState.isRecordingSession) { |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 647 | service.createRecordingSession(callback, sessionState.inputId); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 648 | } else { |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 649 | service.createSession(channels[1], callback, sessionState.inputId); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 650 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 651 | } catch (RemoteException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 652 | Slog.e(TAG, "error in createSession", e); |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 653 | sendSessionTokenToClientLocked(sessionState.client, sessionState.inputId, null, |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 654 | null, sessionState.seq); |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 655 | created = false; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 656 | } |
Jae Seo | 6a6059a | 2014-04-17 21:35:29 -0700 | [diff] [blame] | 657 | channels[1].dispose(); |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 658 | return created; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Sungsoo Lim | d6672b5 | 2014-04-30 10:43:26 +0900 | [diff] [blame] | 661 | private void sendSessionTokenToClientLocked(ITvInputClient client, String inputId, |
Jae Seo | 5c80ad2 | 2014-06-12 19:52:58 -0700 | [diff] [blame] | 662 | IBinder sessionToken, InputChannel channel, int seq) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 663 | try { |
Sungsoo Lim | d6672b5 | 2014-04-30 10:43:26 +0900 | [diff] [blame] | 664 | client.onSessionCreated(inputId, sessionToken, channel, seq); |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 665 | } catch (RemoteException e) { |
| 666 | Slog.e(TAG, "error in onSessionCreated", e); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 667 | } |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 668 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 669 | |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 670 | private void releaseSessionLocked(IBinder sessionToken, int callingUid, int userId) { |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 671 | SessionState sessionState = null; |
| 672 | try { |
| 673 | sessionState = getSessionStateLocked(sessionToken, callingUid, userId); |
| 674 | if (sessionState.session != null) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 675 | UserState userState = getOrCreateUserStateLocked(userId); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 676 | if (sessionToken == userState.mainSessionToken) { |
| 677 | setMainLocked(sessionToken, false, callingUid, userId); |
| 678 | } |
Kyeongkab.Nam | 8d6b71a | 2019-01-16 10:44:37 +0900 | [diff] [blame] | 679 | sessionState.session.asBinder().unlinkToDeath(sessionState, 0); |
Jae Seo | e3c11e8 | 2016-02-08 23:18:49 -0800 | [diff] [blame] | 680 | sessionState.session.release(); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 681 | } |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 682 | } catch (RemoteException | SessionNotFoundException e) { |
| 683 | Slog.e(TAG, "error in releaseSession", e); |
| 684 | } finally { |
| 685 | if (sessionState != null) { |
| 686 | sessionState.session = null; |
| 687 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 688 | } |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 689 | removeSessionStateLocked(sessionToken, userId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Dongwon Kang | fd5b72f | 2014-04-15 17:23:24 +0900 | [diff] [blame] | 692 | private void removeSessionStateLocked(IBinder sessionToken, int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 693 | UserState userState = getOrCreateUserStateLocked(userId); |
Ji-Hwan Lee | abca0ee | 2014-07-24 17:34:19 +0900 | [diff] [blame] | 694 | if (sessionToken == userState.mainSessionToken) { |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 695 | if (DEBUG) { |
| 696 | Slog.d(TAG, "mainSessionToken=null"); |
| 697 | } |
Ji-Hwan Lee | abca0ee | 2014-07-24 17:34:19 +0900 | [diff] [blame] | 698 | userState.mainSessionToken = null; |
| 699 | } |
| 700 | |
| 701 | // Remove the session state from the global session state map of the current user. |
Dongwon Kang | fd5b72f | 2014-04-15 17:23:24 +0900 | [diff] [blame] | 702 | SessionState sessionState = userState.sessionStateMap.remove(sessionToken); |
| 703 | |
Chulwoo Lee | 8d4ded0 | 2014-07-10 03:56:39 +0900 | [diff] [blame] | 704 | if (sessionState == null) { |
| 705 | return; |
| 706 | } |
| 707 | |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 708 | // Also remove the session token from the session token list of the current client and |
| 709 | // service. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 710 | ClientState clientState = userState.clientStateMap.get(sessionState.client.asBinder()); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 711 | if (clientState != null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 712 | clientState.sessionTokens.remove(sessionToken); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 713 | if (clientState.isEmpty()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 714 | userState.clientStateMap.remove(sessionState.client.asBinder()); |
Kyeongkab.Nam | 8d6b71a | 2019-01-16 10:44:37 +0900 | [diff] [blame] | 715 | sessionState.client.asBinder().unlinkToDeath(clientState, 0); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 719 | ServiceState serviceState = userState.serviceStateMap.get(sessionState.componentName); |
| 720 | if (serviceState != null) { |
| 721 | serviceState.sessionTokens.remove(sessionToken); |
Dongwon Kang | fd5b72f | 2014-04-15 17:23:24 +0900 | [diff] [blame] | 722 | } |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 723 | updateServiceConnectionLocked(sessionState.componentName, userId); |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 724 | |
| 725 | // Log the end of watch. |
| 726 | SomeArgs args = SomeArgs.obtain(); |
| 727 | args.arg1 = sessionToken; |
| 728 | args.arg2 = System.currentTimeMillis(); |
| 729 | mWatchLogHandler.obtainMessage(WatchLogHandler.MSG_LOG_WATCH_END, args).sendToTarget(); |
Dongwon Kang | fd5b72f | 2014-04-15 17:23:24 +0900 | [diff] [blame] | 730 | } |
| 731 | |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 732 | private void setMainLocked(IBinder sessionToken, boolean isMain, int callingUid, int userId) { |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 733 | try { |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 734 | SessionState sessionState = getSessionStateLocked(sessionToken, callingUid, userId); |
| 735 | if (sessionState.hardwareSessionToken != null) { |
| 736 | sessionState = getSessionStateLocked(sessionState.hardwareSessionToken, |
| 737 | Process.SYSTEM_UID, userId); |
| 738 | } |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 739 | ServiceState serviceState = getServiceStateLocked(sessionState.componentName, userId); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 740 | if (!serviceState.isHardware) { |
| 741 | return; |
| 742 | } |
| 743 | ITvInputSession session = getSessionLocked(sessionState); |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 744 | session.setMain(isMain); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 745 | } catch (RemoteException | SessionNotFoundException e) { |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 746 | Slog.e(TAG, "error in setMain", e); |
| 747 | } |
| 748 | } |
| 749 | |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 750 | private void notifyInputAddedLocked(UserState userState, String inputId) { |
| 751 | if (DEBUG) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 752 | Slog.d(TAG, "notifyInputAddedLocked(inputId=" + inputId + ")"); |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 753 | } |
| 754 | for (ITvInputManagerCallback callback : userState.callbackSet) { |
| 755 | try { |
| 756 | callback.onInputAdded(inputId); |
| 757 | } catch (RemoteException e) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 758 | Slog.e(TAG, "failed to report added input to callback", e); |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | private void notifyInputRemovedLocked(UserState userState, String inputId) { |
| 764 | if (DEBUG) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 765 | Slog.d(TAG, "notifyInputRemovedLocked(inputId=" + inputId + ")"); |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 766 | } |
| 767 | for (ITvInputManagerCallback callback : userState.callbackSet) { |
| 768 | try { |
| 769 | callback.onInputRemoved(inputId); |
| 770 | } catch (RemoteException e) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 771 | Slog.e(TAG, "failed to report removed input to callback", e); |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 776 | private void notifyInputUpdatedLocked(UserState userState, String inputId) { |
| 777 | if (DEBUG) { |
| 778 | Slog.d(TAG, "notifyInputUpdatedLocked(inputId=" + inputId + ")"); |
| 779 | } |
| 780 | for (ITvInputManagerCallback callback : userState.callbackSet) { |
| 781 | try { |
| 782 | callback.onInputUpdated(inputId); |
| 783 | } catch (RemoteException e) { |
| 784 | Slog.e(TAG, "failed to report updated input to callback", e); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 789 | private void notifyInputStateChangedLocked(UserState userState, String inputId, |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 790 | int state, ITvInputManagerCallback targetCallback) { |
| 791 | if (DEBUG) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 792 | Slog.d(TAG, "notifyInputStateChangedLocked(inputId=" + inputId |
| 793 | + ", state=" + state + ")"); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 794 | } |
| 795 | if (targetCallback == null) { |
| 796 | for (ITvInputManagerCallback callback : userState.callbackSet) { |
| 797 | try { |
| 798 | callback.onInputStateChanged(inputId, state); |
| 799 | } catch (RemoteException e) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 800 | Slog.e(TAG, "failed to report state change to callback", e); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 801 | } |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 802 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 803 | } else { |
| 804 | try { |
| 805 | targetCallback.onInputStateChanged(inputId, state); |
| 806 | } catch (RemoteException e) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 807 | Slog.e(TAG, "failed to report state change to callback", e); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 812 | private void updateTvInputInfoLocked(UserState userState, TvInputInfo inputInfo) { |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 813 | if (DEBUG) { |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 814 | Slog.d(TAG, "updateTvInputInfoLocked(inputInfo=" + inputInfo + ")"); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 815 | } |
Jae Seo | abda420 | 2016-01-28 19:13:04 -0800 | [diff] [blame] | 816 | String inputId = inputInfo.getId(); |
| 817 | TvInputState inputState = userState.inputMap.get(inputId); |
| 818 | if (inputState == null) { |
| 819 | Slog.e(TAG, "failed to set input info - unknown input id " + inputId); |
| 820 | return; |
| 821 | } |
Jae Seo | abda420 | 2016-01-28 19:13:04 -0800 | [diff] [blame] | 822 | inputState.info = inputInfo; |
| 823 | |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 824 | for (ITvInputManagerCallback callback : userState.callbackSet) { |
| 825 | try { |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 826 | callback.onTvInputInfoUpdated(inputInfo); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 827 | } catch (RemoteException e) { |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 828 | Slog.e(TAG, "failed to report updated input info to callback", e); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 833 | private void setStateLocked(String inputId, int state, int userId) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 834 | UserState userState = getOrCreateUserStateLocked(userId); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 835 | TvInputState inputState = userState.inputMap.get(inputId); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 836 | ServiceState serviceState = userState.serviceStateMap.get(inputState.info.getComponent()); |
| 837 | int oldState = inputState.state; |
| 838 | inputState.state = state; |
| 839 | if (serviceState != null && serviceState.service == null |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 840 | && (!serviceState.sessionTokens.isEmpty() || serviceState.isHardware)) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 841 | // We don't notify state change while reconnecting. It should remain disconnected. |
| 842 | return; |
| 843 | } |
| 844 | if (oldState != state) { |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 845 | notifyInputStateChangedLocked(userState, inputId, state, null); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 849 | private final class BinderService extends ITvInputManager.Stub { |
| 850 | @Override |
| 851 | public List<TvInputInfo> getTvInputList(int userId) { |
| 852 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 853 | Binder.getCallingUid(), userId, "getTvInputList"); |
| 854 | final long identity = Binder.clearCallingIdentity(); |
| 855 | try { |
| 856 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 857 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 858 | List<TvInputInfo> inputList = new ArrayList<>(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 859 | for (TvInputState state : userState.inputMap.values()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 860 | inputList.add(state.info); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 861 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 862 | return inputList; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 863 | } |
| 864 | } finally { |
| 865 | Binder.restoreCallingIdentity(identity); |
| 866 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | @Override |
Jae Seo | b375805 | 2014-07-12 19:25:24 -0700 | [diff] [blame] | 870 | public TvInputInfo getTvInputInfo(String inputId, int userId) { |
| 871 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 872 | Binder.getCallingUid(), userId, "getTvInputInfo"); |
| 873 | final long identity = Binder.clearCallingIdentity(); |
| 874 | try { |
| 875 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 876 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | b375805 | 2014-07-12 19:25:24 -0700 | [diff] [blame] | 877 | TvInputState state = userState.inputMap.get(inputId); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 878 | return state == null ? null : state.info; |
Jae Seo | b375805 | 2014-07-12 19:25:24 -0700 | [diff] [blame] | 879 | } |
| 880 | } finally { |
| 881 | Binder.restoreCallingIdentity(identity); |
| 882 | } |
| 883 | } |
| 884 | |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 885 | public void updateTvInputInfo(TvInputInfo inputInfo, int userId) { |
Jae Seo | c2a8951 | 2016-01-28 10:38:11 -0800 | [diff] [blame] | 886 | String inputInfoPackageName = inputInfo.getServiceInfo().packageName; |
| 887 | String callingPackageName = getCallingPackageName(); |
Dongwon Kang | 9d0e0f1 | 2016-10-11 15:14:01 -0700 | [diff] [blame] | 888 | if (!TextUtils.equals(inputInfoPackageName, callingPackageName) |
| 889 | && mContext.checkCallingPermission( |
| 890 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 891 | != PackageManager.PERMISSION_GRANTED) { |
| 892 | // Only the app owning the input and system settings are allowed to update info. |
Jae Seo | c2a8951 | 2016-01-28 10:38:11 -0800 | [diff] [blame] | 893 | throw new IllegalArgumentException("calling package " + callingPackageName |
| 894 | + " is not allowed to change TvInputInfo for " + inputInfoPackageName); |
| 895 | } |
| 896 | |
| 897 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 898 | Binder.getCallingUid(), userId, "updateTvInputInfo"); |
Jae Seo | c2a8951 | 2016-01-28 10:38:11 -0800 | [diff] [blame] | 899 | final long identity = Binder.clearCallingIdentity(); |
| 900 | try { |
| 901 | synchronized (mLock) { |
| 902 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | aa5605f | 2016-02-13 01:38:08 -0800 | [diff] [blame] | 903 | updateTvInputInfoLocked(userState, inputInfo); |
Jae Seo | c2a8951 | 2016-01-28 10:38:11 -0800 | [diff] [blame] | 904 | } |
| 905 | } finally { |
| 906 | Binder.restoreCallingIdentity(identity); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | private String getCallingPackageName() { |
| 911 | final String[] packages = mContext.getPackageManager().getPackagesForUid( |
| 912 | Binder.getCallingUid()); |
| 913 | if (packages != null && packages.length > 0) { |
| 914 | return packages[0]; |
| 915 | } |
| 916 | return "unknown"; |
| 917 | } |
| 918 | |
Jae Seo | b375805 | 2014-07-12 19:25:24 -0700 | [diff] [blame] | 919 | @Override |
Dongwon Kang | 993f81e | 2014-11-27 19:34:18 +0900 | [diff] [blame] | 920 | public int getTvInputState(String inputId, int userId) { |
| 921 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 922 | Binder.getCallingUid(), userId, "getTvInputState"); |
| 923 | final long identity = Binder.clearCallingIdentity(); |
| 924 | try { |
| 925 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 926 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Dongwon Kang | 993f81e | 2014-11-27 19:34:18 +0900 | [diff] [blame] | 927 | TvInputState state = userState.inputMap.get(inputId); |
Jae Seo | 82fce64 | 2015-04-20 15:37:50 -0700 | [diff] [blame] | 928 | return state == null ? INPUT_STATE_CONNECTED : state.state; |
Dongwon Kang | 993f81e | 2014-11-27 19:34:18 +0900 | [diff] [blame] | 929 | } |
| 930 | } finally { |
| 931 | Binder.restoreCallingIdentity(identity); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | @Override |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 936 | public List<TvContentRatingSystemInfo> getTvContentRatingSystemList(int userId) { |
Shubang | dd3ec0b | 2017-06-23 13:54:10 -0700 | [diff] [blame] | 937 | if (mContext.checkCallingPermission( |
| 938 | android.Manifest.permission.READ_CONTENT_RATING_SYSTEMS) |
| 939 | != PackageManager.PERMISSION_GRANTED) { |
| 940 | throw new SecurityException( |
| 941 | "The caller does not have permission to read content rating systems"); |
| 942 | } |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 943 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 944 | Binder.getCallingUid(), userId, "getTvContentRatingSystemList"); |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 945 | final long identity = Binder.clearCallingIdentity(); |
| 946 | try { |
| 947 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 948 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 949 | return userState.contentRatingSystemList; |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 950 | } |
| 951 | } finally { |
| 952 | Binder.restoreCallingIdentity(identity); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | @Override |
Conrad Chen | 558acf9 | 2017-04-05 17:31:01 -0700 | [diff] [blame] | 957 | public void sendTvInputNotifyIntent(Intent intent, int userId) { |
| 958 | if (mContext.checkCallingPermission(android.Manifest.permission.NOTIFY_TV_INPUTS) |
| 959 | != PackageManager.PERMISSION_GRANTED) { |
| 960 | throw new SecurityException("The caller: " + getCallingPackageName() |
| 961 | + " doesn't have permission: " |
| 962 | + android.Manifest.permission.NOTIFY_TV_INPUTS); |
| 963 | } |
| 964 | if (TextUtils.isEmpty(intent.getPackage())) { |
| 965 | throw new IllegalArgumentException("Must specify package name to notify."); |
| 966 | } |
| 967 | switch (intent.getAction()) { |
| 968 | case TvContract.ACTION_PREVIEW_PROGRAM_BROWSABLE_DISABLED: |
| 969 | if (intent.getLongExtra(TvContract.EXTRA_PREVIEW_PROGRAM_ID, -1) < 0) { |
| 970 | throw new IllegalArgumentException("Invalid preview program ID."); |
| 971 | } |
| 972 | break; |
| 973 | case TvContract.ACTION_WATCH_NEXT_PROGRAM_BROWSABLE_DISABLED: |
| 974 | if (intent.getLongExtra(TvContract.EXTRA_WATCH_NEXT_PROGRAM_ID, -1) < 0) { |
| 975 | throw new IllegalArgumentException("Invalid watch next program ID."); |
| 976 | } |
| 977 | break; |
| 978 | case TvContract.ACTION_PREVIEW_PROGRAM_ADDED_TO_WATCH_NEXT: |
| 979 | if (intent.getLongExtra(TvContract.EXTRA_PREVIEW_PROGRAM_ID, -1) < 0) { |
| 980 | throw new IllegalArgumentException("Invalid preview program ID."); |
| 981 | } |
| 982 | if (intent.getLongExtra(TvContract.EXTRA_WATCH_NEXT_PROGRAM_ID, -1) < 0) { |
| 983 | throw new IllegalArgumentException("Invalid watch next program ID."); |
| 984 | } |
| 985 | break; |
| 986 | default: |
| 987 | throw new IllegalArgumentException("Invalid TV input notifying action: " |
| 988 | + intent.getAction()); |
| 989 | } |
| 990 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 991 | Binder.getCallingUid(), userId, "sendTvInputNotifyIntent"); |
| 992 | final long identity = Binder.clearCallingIdentity(); |
| 993 | try { |
| 994 | getContext().sendBroadcastAsUser(intent, new UserHandle(resolvedUserId)); |
| 995 | } finally { |
| 996 | Binder.restoreCallingIdentity(identity); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | @Override |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1001 | public void registerCallback(final ITvInputManagerCallback callback, int userId) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1002 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1003 | Binder.getCallingUid(), userId, "registerCallback"); |
| 1004 | final long identity = Binder.clearCallingIdentity(); |
| 1005 | try { |
| 1006 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1007 | final UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1008 | userState.callbackSet.add(callback); |
Kyeongkab.Nam | 8d6b71a | 2019-01-16 10:44:37 +0900 | [diff] [blame] | 1009 | mDeathRecipient = new IBinder.DeathRecipient() { |
| 1010 | @Override |
| 1011 | public void binderDied() { |
| 1012 | synchronized (mLock) { |
| 1013 | if (userState.callbackSet != null) { |
| 1014 | userState.callbackSet.remove(callback); |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
Kyeongkab.Nam | 8d6b71a | 2019-01-16 10:44:37 +0900 | [diff] [blame] | 1017 | } |
| 1018 | }; |
| 1019 | |
| 1020 | try { |
| 1021 | callback.asBinder().linkToDeath(mDeathRecipient, 0); |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 1022 | } catch (RemoteException e) { |
| 1023 | Slog.e(TAG, "client process has already died", e); |
| 1024 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1025 | } |
| 1026 | } finally { |
| 1027 | Binder.restoreCallingIdentity(identity); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | @Override |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1032 | public void unregisterCallback(ITvInputManagerCallback callback, int userId) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1033 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1034 | Binder.getCallingUid(), userId, "unregisterCallback"); |
| 1035 | final long identity = Binder.clearCallingIdentity(); |
| 1036 | try { |
| 1037 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1038 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1039 | userState.callbackSet.remove(callback); |
Kyeongkab.Nam | 8d6b71a | 2019-01-16 10:44:37 +0900 | [diff] [blame] | 1040 | callback.asBinder().unlinkToDeath(mDeathRecipient, 0); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } finally { |
| 1043 | Binder.restoreCallingIdentity(identity); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | @Override |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1048 | public boolean isParentalControlsEnabled(int userId) { |
| 1049 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1050 | Binder.getCallingUid(), userId, "isParentalControlsEnabled"); |
| 1051 | final long identity = Binder.clearCallingIdentity(); |
| 1052 | try { |
| 1053 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1054 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1055 | return userState.persistentDataStore.isParentalControlsEnabled(); |
| 1056 | } |
| 1057 | } finally { |
| 1058 | Binder.restoreCallingIdentity(identity); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | @Override |
| 1063 | public void setParentalControlsEnabled(boolean enabled, int userId) { |
| 1064 | ensureParentalControlsPermission(); |
| 1065 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1066 | Binder.getCallingUid(), userId, "setParentalControlsEnabled"); |
| 1067 | final long identity = Binder.clearCallingIdentity(); |
| 1068 | try { |
| 1069 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1070 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1071 | userState.persistentDataStore.setParentalControlsEnabled(enabled); |
| 1072 | } |
| 1073 | } finally { |
| 1074 | Binder.restoreCallingIdentity(identity); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | @Override |
| 1079 | public boolean isRatingBlocked(String rating, int userId) { |
| 1080 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1081 | Binder.getCallingUid(), userId, "isRatingBlocked"); |
| 1082 | final long identity = Binder.clearCallingIdentity(); |
| 1083 | try { |
| 1084 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1085 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1086 | return userState.persistentDataStore.isRatingBlocked( |
| 1087 | TvContentRating.unflattenFromString(rating)); |
| 1088 | } |
| 1089 | } finally { |
| 1090 | Binder.restoreCallingIdentity(identity); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
| 1095 | public List<String> getBlockedRatings(int userId) { |
| 1096 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1097 | Binder.getCallingUid(), userId, "getBlockedRatings"); |
| 1098 | final long identity = Binder.clearCallingIdentity(); |
| 1099 | try { |
| 1100 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1101 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 1102 | List<String> ratings = new ArrayList<>(); |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1103 | for (TvContentRating rating |
| 1104 | : userState.persistentDataStore.getBlockedRatings()) { |
| 1105 | ratings.add(rating.flattenToString()); |
| 1106 | } |
| 1107 | return ratings; |
| 1108 | } |
| 1109 | } finally { |
| 1110 | Binder.restoreCallingIdentity(identity); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | @Override |
| 1115 | public void addBlockedRating(String rating, int userId) { |
| 1116 | ensureParentalControlsPermission(); |
| 1117 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1118 | Binder.getCallingUid(), userId, "addBlockedRating"); |
| 1119 | final long identity = Binder.clearCallingIdentity(); |
| 1120 | try { |
| 1121 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1122 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1123 | userState.persistentDataStore.addBlockedRating( |
| 1124 | TvContentRating.unflattenFromString(rating)); |
| 1125 | } |
| 1126 | } finally { |
| 1127 | Binder.restoreCallingIdentity(identity); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | @Override |
| 1132 | public void removeBlockedRating(String rating, int userId) { |
| 1133 | ensureParentalControlsPermission(); |
| 1134 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), |
| 1135 | Binder.getCallingUid(), userId, "removeBlockedRating"); |
| 1136 | final long identity = Binder.clearCallingIdentity(); |
| 1137 | try { |
| 1138 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1139 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1140 | userState.persistentDataStore.removeBlockedRating( |
| 1141 | TvContentRating.unflattenFromString(rating)); |
| 1142 | } |
| 1143 | } finally { |
| 1144 | Binder.restoreCallingIdentity(identity); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | private void ensureParentalControlsPermission() { |
Jae Seo | fc836f6 | 2014-08-27 00:47:56 +0000 | [diff] [blame] | 1149 | if (mContext.checkCallingPermission( |
| 1150 | android.Manifest.permission.MODIFY_PARENTAL_CONTROLS) |
| 1151 | != PackageManager.PERMISSION_GRANTED) { |
| 1152 | throw new SecurityException( |
| 1153 | "The caller does not have parental controls permission"); |
| 1154 | } |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | @Override |
Sungsoo Lim | d6672b5 | 2014-04-30 10:43:26 +0900 | [diff] [blame] | 1158 | public void createSession(final ITvInputClient client, final String inputId, |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 1159 | boolean isRecordingSession, int seq, int userId) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1160 | final int callingUid = Binder.getCallingUid(); |
| 1161 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1162 | userId, "createSession"); |
| 1163 | final long identity = Binder.clearCallingIdentity(); |
| 1164 | try { |
| 1165 | synchronized (mLock) { |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 1166 | if (userId != mCurrentUserId && !isRecordingSession) { |
| 1167 | // A non-recording session of a backgroud (non-current) user |
| 1168 | // should not be created. |
| 1169 | // Let the client get onConnectionFailed callback for this case. |
| 1170 | sendSessionTokenToClientLocked(client, inputId, null, null, seq); |
| 1171 | return; |
| 1172 | } |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1173 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 1174 | TvInputState inputState = userState.inputMap.get(inputId); |
| 1175 | if (inputState == null) { |
| 1176 | Slog.w(TAG, "Failed to find input state for inputId=" + inputId); |
| 1177 | sendSessionTokenToClientLocked(client, inputId, null, null, seq); |
| 1178 | return; |
| 1179 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1180 | TvInputInfo info = inputState.info; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1181 | ServiceState serviceState = userState.serviceStateMap.get(info.getComponent()); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1182 | if (serviceState == null) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1183 | serviceState = new ServiceState(info.getComponent(), resolvedUserId); |
| 1184 | userState.serviceStateMap.put(info.getComponent(), serviceState); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1185 | } |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 1186 | // Send a null token immediately while reconnecting. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 1187 | if (serviceState.reconnecting) { |
Jae Seo | 5c80ad2 | 2014-06-12 19:52:58 -0700 | [diff] [blame] | 1188 | sendSessionTokenToClientLocked(client, inputId, null, null, seq); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | // Create a new session token and a session state. |
| 1193 | IBinder sessionToken = new Binder(); |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 1194 | SessionState sessionState = new SessionState(sessionToken, info.getId(), |
| 1195 | info.getComponent(), isRecordingSession, client, seq, callingUid, |
| 1196 | resolvedUserId); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 1197 | |
| 1198 | // Add them to the global session state map of the current user. |
| 1199 | userState.sessionStateMap.put(sessionToken, sessionState); |
| 1200 | |
| 1201 | // Also, add them to the session state map of the current service. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1202 | serviceState.sessionTokens.add(sessionToken); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1203 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1204 | if (serviceState.service != null) { |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 1205 | if (!createSessionInternalLocked(serviceState.service, sessionToken, |
| 1206 | resolvedUserId)) { |
| 1207 | removeSessionStateLocked(sessionToken, resolvedUserId); |
| 1208 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1209 | } else { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1210 | updateServiceConnectionLocked(info.getComponent(), resolvedUserId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | } finally { |
| 1214 | Binder.restoreCallingIdentity(identity); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | @Override |
| 1219 | public void releaseSession(IBinder sessionToken, int userId) { |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1220 | if (DEBUG) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 1221 | Slog.d(TAG, "releaseSession(sessionToken=" + sessionToken + ")"); |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1222 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1223 | final int callingUid = Binder.getCallingUid(); |
| 1224 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1225 | userId, "releaseSession"); |
| 1226 | final long identity = Binder.clearCallingIdentity(); |
| 1227 | try { |
| 1228 | synchronized (mLock) { |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 1229 | releaseSessionLocked(sessionToken, callingUid, resolvedUserId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1230 | } |
| 1231 | } finally { |
| 1232 | Binder.restoreCallingIdentity(identity); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | @Override |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 1237 | public void setMainSession(IBinder sessionToken, int userId) { |
Shubang | e41b76f | 2017-06-07 13:23:12 -0700 | [diff] [blame] | 1238 | if (mContext.checkCallingPermission( |
| 1239 | android.Manifest.permission.CHANGE_HDMI_CEC_ACTIVE_SOURCE) |
| 1240 | != PackageManager.PERMISSION_GRANTED) { |
| 1241 | throw new SecurityException( |
| 1242 | "The caller does not have CHANGE_HDMI_CEC_ACTIVE_SOURCE permission"); |
| 1243 | } |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1244 | if (DEBUG) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 1245 | Slog.d(TAG, "setMainSession(sessionToken=" + sessionToken + ")"); |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1246 | } |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 1247 | final int callingUid = Binder.getCallingUid(); |
| 1248 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1249 | userId, "setMainSession"); |
| 1250 | final long identity = Binder.clearCallingIdentity(); |
| 1251 | try { |
| 1252 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1253 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Ji-Hwan Lee | 956afc2 | 2014-07-26 11:31:39 +0900 | [diff] [blame] | 1254 | if (userState.mainSessionToken == sessionToken) { |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 1255 | return; |
| 1256 | } |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1257 | if (DEBUG) { |
| 1258 | Slog.d(TAG, "mainSessionToken=" + sessionToken); |
Ji-Hwan Lee | 956afc2 | 2014-07-26 11:31:39 +0900 | [diff] [blame] | 1259 | } |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1260 | IBinder oldMainSessionToken = userState.mainSessionToken; |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 1261 | userState.mainSessionToken = sessionToken; |
| 1262 | |
Ji-Hwan Lee | 956afc2 | 2014-07-26 11:31:39 +0900 | [diff] [blame] | 1263 | // Inform the new main session first. |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1264 | // See {@link TvInputService.Session#onSetMain}. |
| 1265 | if (sessionToken != null) { |
| 1266 | setMainLocked(sessionToken, true, callingUid, userId); |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 1267 | } |
Ji-Hwan Lee | 15c56aa | 2014-08-18 22:01:55 +0900 | [diff] [blame] | 1268 | if (oldMainSessionToken != null) { |
| 1269 | setMainLocked(oldMainSessionToken, false, Process.SYSTEM_UID, userId); |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | } finally { |
| 1273 | Binder.restoreCallingIdentity(identity); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | @Override |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1278 | public void setSurface(IBinder sessionToken, Surface surface, int userId) { |
| 1279 | final int callingUid = Binder.getCallingUid(); |
| 1280 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1281 | userId, "setSurface"); |
| 1282 | final long identity = Binder.clearCallingIdentity(); |
| 1283 | try { |
| 1284 | synchronized (mLock) { |
| 1285 | try { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1286 | SessionState sessionState = getSessionStateLocked(sessionToken, callingUid, |
| 1287 | resolvedUserId); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1288 | if (sessionState.hardwareSessionToken == null) { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1289 | getSessionLocked(sessionState).setSurface(surface); |
| 1290 | } else { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1291 | getSessionLocked(sessionState.hardwareSessionToken, |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1292 | Process.SYSTEM_UID, resolvedUserId).setSurface(surface); |
| 1293 | } |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1294 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1295 | Slog.e(TAG, "error in setSurface", e); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } finally { |
Youngsang Cho | f836206 | 2014-04-30 17:24:20 +0900 | [diff] [blame] | 1299 | if (surface != null) { |
| 1300 | // surface is not used in TvInputManagerService. |
| 1301 | surface.release(); |
| 1302 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1303 | Binder.restoreCallingIdentity(identity); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | @Override |
Youngsang Cho | e821d71 | 2014-07-16 14:22:19 -0700 | [diff] [blame] | 1308 | public void dispatchSurfaceChanged(IBinder sessionToken, int format, int width, |
| 1309 | int height, int userId) { |
| 1310 | final int callingUid = Binder.getCallingUid(); |
| 1311 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1312 | userId, "dispatchSurfaceChanged"); |
| 1313 | final long identity = Binder.clearCallingIdentity(); |
| 1314 | try { |
| 1315 | synchronized (mLock) { |
| 1316 | try { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1317 | SessionState sessionState = getSessionStateLocked(sessionToken, callingUid, |
| 1318 | resolvedUserId); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1319 | getSessionLocked(sessionState).dispatchSurfaceChanged(format, width, |
| 1320 | height); |
| 1321 | if (sessionState.hardwareSessionToken != null) { |
| 1322 | getSessionLocked(sessionState.hardwareSessionToken, Process.SYSTEM_UID, |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1323 | resolvedUserId).dispatchSurfaceChanged(format, width, height); |
| 1324 | } |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1325 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | e821d71 | 2014-07-16 14:22:19 -0700 | [diff] [blame] | 1326 | Slog.e(TAG, "error in dispatchSurfaceChanged", e); |
| 1327 | } |
| 1328 | } |
| 1329 | } finally { |
| 1330 | Binder.restoreCallingIdentity(identity); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | @Override |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1335 | public void setVolume(IBinder sessionToken, float volume, int userId) { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1336 | final float REMOTE_VOLUME_ON = 1.0f; |
| 1337 | final float REMOTE_VOLUME_OFF = 0f; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1338 | final int callingUid = Binder.getCallingUid(); |
| 1339 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1340 | userId, "setVolume"); |
| 1341 | final long identity = Binder.clearCallingIdentity(); |
| 1342 | try { |
| 1343 | synchronized (mLock) { |
| 1344 | try { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1345 | SessionState sessionState = getSessionStateLocked(sessionToken, callingUid, |
| 1346 | resolvedUserId); |
| 1347 | getSessionLocked(sessionState).setVolume(volume); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1348 | if (sessionState.hardwareSessionToken != null) { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1349 | // Here, we let the hardware session know only whether volume is on or |
| 1350 | // off to prevent that the volume is controlled in the both side. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1351 | getSessionLocked(sessionState.hardwareSessionToken, |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1352 | Process.SYSTEM_UID, resolvedUserId).setVolume((volume > 0.0f) |
| 1353 | ? REMOTE_VOLUME_ON : REMOTE_VOLUME_OFF); |
| 1354 | } |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1355 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1356 | Slog.e(TAG, "error in setVolume", e); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | } finally { |
| 1360 | Binder.restoreCallingIdentity(identity); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | @Override |
Sungsoo Lim | 1a6b25e | 2014-07-09 10:40:43 +0900 | [diff] [blame] | 1365 | public void tune(IBinder sessionToken, final Uri channelUri, Bundle params, int userId) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1366 | final int callingUid = Binder.getCallingUid(); |
| 1367 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1368 | userId, "tune"); |
| 1369 | final long identity = Binder.clearCallingIdentity(); |
| 1370 | try { |
| 1371 | synchronized (mLock) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1372 | try { |
Sungsoo Lim | 1a6b25e | 2014-07-09 10:40:43 +0900 | [diff] [blame] | 1373 | getSessionLocked(sessionToken, callingUid, resolvedUserId).tune( |
| 1374 | channelUri, params); |
Jae Seo | c22d0c0 | 2014-08-15 13:03:21 -0700 | [diff] [blame] | 1375 | if (TvContract.isChannelUriForPassthroughInput(channelUri)) { |
Youngsang Cho | 008f6d4 | 2014-07-22 21:29:47 -0700 | [diff] [blame] | 1376 | // Do not log the watch history for passthrough inputs. |
| 1377 | return; |
| 1378 | } |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 1379 | |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1380 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 1381 | SessionState sessionState = userState.sessionStateMap.get(sessionToken); |
Jae Seo | e3c11e8 | 2016-02-08 23:18:49 -0800 | [diff] [blame] | 1382 | if (sessionState.isRecordingSession) { |
| 1383 | return; |
| 1384 | } |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 1385 | |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 1386 | // Log the start of watch. |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 1387 | SomeArgs args = SomeArgs.obtain(); |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 1388 | args.arg1 = sessionState.componentName.getPackageName(); |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 1389 | args.arg2 = System.currentTimeMillis(); |
| 1390 | args.arg3 = ContentUris.parseId(channelUri); |
| 1391 | args.arg4 = params; |
| 1392 | args.arg5 = sessionToken; |
| 1393 | mWatchLogHandler.obtainMessage(WatchLogHandler.MSG_LOG_WATCH_START, args) |
| 1394 | .sendToTarget(); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1395 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1396 | Slog.e(TAG, "error in tune", e); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | } finally { |
| 1400 | Binder.restoreCallingIdentity(identity); |
| 1401 | } |
| 1402 | } |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1403 | |
| 1404 | @Override |
Jae Seo | a903383 | 2015-03-11 19:29:46 -0700 | [diff] [blame] | 1405 | public void unblockContent( |
Sungsoo Lim | 9bf671f | 2014-07-19 12:59:51 +0900 | [diff] [blame] | 1406 | IBinder sessionToken, String unblockedRating, int userId) { |
Dongwon Kang | e12d810 | 2016-03-04 16:45:39 -0800 | [diff] [blame] | 1407 | ensureParentalControlsPermission(); |
Jaewan Kim | 903d6b7 | 2014-07-16 11:28:56 +0900 | [diff] [blame] | 1408 | final int callingUid = Binder.getCallingUid(); |
| 1409 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1410 | userId, "unblockContent"); |
| 1411 | final long identity = Binder.clearCallingIdentity(); |
| 1412 | try { |
| 1413 | synchronized (mLock) { |
| 1414 | try { |
| 1415 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
Jae Seo | a903383 | 2015-03-11 19:29:46 -0700 | [diff] [blame] | 1416 | .unblockContent(unblockedRating); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1417 | } catch (RemoteException | SessionNotFoundException e) { |
Jae Seo | a903383 | 2015-03-11 19:29:46 -0700 | [diff] [blame] | 1418 | Slog.e(TAG, "error in unblockContent", e); |
Jaewan Kim | 903d6b7 | 2014-07-16 11:28:56 +0900 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | } finally { |
| 1422 | Binder.restoreCallingIdentity(identity); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | @Override |
Jae Seo | 2c1c31c | 2014-07-10 14:57:01 -0700 | [diff] [blame] | 1427 | public void setCaptionEnabled(IBinder sessionToken, boolean enabled, int userId) { |
| 1428 | final int callingUid = Binder.getCallingUid(); |
| 1429 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1430 | userId, "setCaptionEnabled"); |
| 1431 | final long identity = Binder.clearCallingIdentity(); |
| 1432 | try { |
| 1433 | synchronized (mLock) { |
| 1434 | try { |
| 1435 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1436 | .setCaptionEnabled(enabled); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1437 | } catch (RemoteException | SessionNotFoundException e) { |
Jae Seo | 2c1c31c | 2014-07-10 14:57:01 -0700 | [diff] [blame] | 1438 | Slog.e(TAG, "error in setCaptionEnabled", e); |
| 1439 | } |
| 1440 | } |
| 1441 | } finally { |
| 1442 | Binder.restoreCallingIdentity(identity); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | @Override |
Jae Seo | 10d285a | 2014-07-31 22:46:47 +0900 | [diff] [blame] | 1447 | public void selectTrack(IBinder sessionToken, int type, String trackId, int userId) { |
Dongwon Kang | 1f21391 | 2014-07-02 18:35:08 +0900 | [diff] [blame] | 1448 | final int callingUid = Binder.getCallingUid(); |
| 1449 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1450 | userId, "selectTrack"); |
| 1451 | final long identity = Binder.clearCallingIdentity(); |
| 1452 | try { |
| 1453 | synchronized (mLock) { |
| 1454 | try { |
| 1455 | getSessionLocked(sessionToken, callingUid, resolvedUserId).selectTrack( |
Jae Seo | 10d285a | 2014-07-31 22:46:47 +0900 | [diff] [blame] | 1456 | type, trackId); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1457 | } catch (RemoteException | SessionNotFoundException e) { |
Dongwon Kang | 1f21391 | 2014-07-02 18:35:08 +0900 | [diff] [blame] | 1458 | Slog.e(TAG, "error in selectTrack", e); |
| 1459 | } |
| 1460 | } |
| 1461 | } finally { |
| 1462 | Binder.restoreCallingIdentity(identity); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | @Override |
Jae Seo | a759b11 | 2014-07-18 22:16:08 -0700 | [diff] [blame] | 1467 | public void sendAppPrivateCommand(IBinder sessionToken, String command, Bundle data, |
| 1468 | int userId) { |
| 1469 | final int callingUid = Binder.getCallingUid(); |
| 1470 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1471 | userId, "sendAppPrivateCommand"); |
| 1472 | final long identity = Binder.clearCallingIdentity(); |
| 1473 | try { |
| 1474 | synchronized (mLock) { |
| 1475 | try { |
| 1476 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1477 | .appPrivateCommand(command, data); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1478 | } catch (RemoteException | SessionNotFoundException e) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 1479 | Slog.e(TAG, "error in appPrivateCommand", e); |
Jae Seo | a759b11 | 2014-07-18 22:16:08 -0700 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | } finally { |
| 1483 | Binder.restoreCallingIdentity(identity); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | @Override |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1488 | public void createOverlayView(IBinder sessionToken, IBinder windowToken, Rect frame, |
| 1489 | int userId) { |
| 1490 | final int callingUid = Binder.getCallingUid(); |
| 1491 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1492 | userId, "createOverlayView"); |
| 1493 | final long identity = Binder.clearCallingIdentity(); |
| 1494 | try { |
| 1495 | synchronized (mLock) { |
| 1496 | try { |
| 1497 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1498 | .createOverlayView(windowToken, frame); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1499 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1500 | Slog.e(TAG, "error in createOverlayView", e); |
| 1501 | } |
| 1502 | } |
| 1503 | } finally { |
| 1504 | Binder.restoreCallingIdentity(identity); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | @Override |
| 1509 | public void relayoutOverlayView(IBinder sessionToken, Rect frame, int userId) { |
| 1510 | final int callingUid = Binder.getCallingUid(); |
| 1511 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1512 | userId, "relayoutOverlayView"); |
| 1513 | final long identity = Binder.clearCallingIdentity(); |
| 1514 | try { |
| 1515 | synchronized (mLock) { |
| 1516 | try { |
| 1517 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1518 | .relayoutOverlayView(frame); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1519 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1520 | Slog.e(TAG, "error in relayoutOverlayView", e); |
| 1521 | } |
| 1522 | } |
| 1523 | } finally { |
| 1524 | Binder.restoreCallingIdentity(identity); |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | @Override |
| 1529 | public void removeOverlayView(IBinder sessionToken, int userId) { |
| 1530 | final int callingUid = Binder.getCallingUid(); |
| 1531 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1532 | userId, "removeOverlayView"); |
| 1533 | final long identity = Binder.clearCallingIdentity(); |
| 1534 | try { |
| 1535 | synchronized (mLock) { |
| 1536 | try { |
| 1537 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1538 | .removeOverlayView(); |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 1539 | } catch (RemoteException | SessionNotFoundException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 1540 | Slog.e(TAG, "error in removeOverlayView", e); |
| 1541 | } |
| 1542 | } |
| 1543 | } finally { |
| 1544 | Binder.restoreCallingIdentity(identity); |
| 1545 | } |
| 1546 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1547 | |
| 1548 | @Override |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 1549 | public void timeShiftPlay(IBinder sessionToken, final Uri recordedProgramUri, int userId) { |
| 1550 | final int callingUid = Binder.getCallingUid(); |
| 1551 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1552 | userId, "timeShiftPlay"); |
| 1553 | final long identity = Binder.clearCallingIdentity(); |
| 1554 | try { |
| 1555 | synchronized (mLock) { |
| 1556 | try { |
| 1557 | getSessionLocked(sessionToken, callingUid, resolvedUserId).timeShiftPlay( |
| 1558 | recordedProgramUri); |
| 1559 | } catch (RemoteException | SessionNotFoundException e) { |
| 1560 | Slog.e(TAG, "error in timeShiftPlay", e); |
| 1561 | } |
| 1562 | } |
| 1563 | } finally { |
| 1564 | Binder.restoreCallingIdentity(identity); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | @Override |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1569 | public void timeShiftPause(IBinder sessionToken, int userId) { |
| 1570 | final int callingUid = Binder.getCallingUid(); |
| 1571 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1572 | userId, "timeShiftPause"); |
| 1573 | final long identity = Binder.clearCallingIdentity(); |
| 1574 | try { |
| 1575 | synchronized (mLock) { |
| 1576 | try { |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 1577 | getSessionLocked(sessionToken, callingUid, resolvedUserId).timeShiftPause(); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1578 | } catch (RemoteException | SessionNotFoundException e) { |
| 1579 | Slog.e(TAG, "error in timeShiftPause", e); |
| 1580 | } |
| 1581 | } |
| 1582 | } finally { |
| 1583 | Binder.restoreCallingIdentity(identity); |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | @Override |
| 1588 | public void timeShiftResume(IBinder sessionToken, int userId) { |
| 1589 | final int callingUid = Binder.getCallingUid(); |
| 1590 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1591 | userId, "timeShiftResume"); |
| 1592 | final long identity = Binder.clearCallingIdentity(); |
| 1593 | try { |
| 1594 | synchronized (mLock) { |
| 1595 | try { |
| 1596 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1597 | .timeShiftResume(); |
| 1598 | } catch (RemoteException | SessionNotFoundException e) { |
| 1599 | Slog.e(TAG, "error in timeShiftResume", e); |
| 1600 | } |
| 1601 | } |
| 1602 | } finally { |
| 1603 | Binder.restoreCallingIdentity(identity); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | @Override |
| 1608 | public void timeShiftSeekTo(IBinder sessionToken, long timeMs, int userId) { |
| 1609 | final int callingUid = Binder.getCallingUid(); |
| 1610 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1611 | userId, "timeShiftSeekTo"); |
| 1612 | final long identity = Binder.clearCallingIdentity(); |
| 1613 | try { |
| 1614 | synchronized (mLock) { |
| 1615 | try { |
| 1616 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
| 1617 | .timeShiftSeekTo(timeMs); |
| 1618 | } catch (RemoteException | SessionNotFoundException e) { |
| 1619 | Slog.e(TAG, "error in timeShiftSeekTo", e); |
| 1620 | } |
| 1621 | } |
| 1622 | } finally { |
| 1623 | Binder.restoreCallingIdentity(identity); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | @Override |
Jae Seo | 4b34cc7 | 2015-05-15 17:29:39 -0700 | [diff] [blame] | 1628 | public void timeShiftSetPlaybackParams(IBinder sessionToken, PlaybackParams params, |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1629 | int userId) { |
| 1630 | final int callingUid = Binder.getCallingUid(); |
| 1631 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
Jae Seo | 4b34cc7 | 2015-05-15 17:29:39 -0700 | [diff] [blame] | 1632 | userId, "timeShiftSetPlaybackParams"); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1633 | final long identity = Binder.clearCallingIdentity(); |
| 1634 | try { |
| 1635 | synchronized (mLock) { |
| 1636 | try { |
| 1637 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
Jae Seo | 4b34cc7 | 2015-05-15 17:29:39 -0700 | [diff] [blame] | 1638 | .timeShiftSetPlaybackParams(params); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1639 | } catch (RemoteException | SessionNotFoundException e) { |
Jae Seo | 4b34cc7 | 2015-05-15 17:29:39 -0700 | [diff] [blame] | 1640 | Slog.e(TAG, "error in timeShiftSetPlaybackParams", e); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | } finally { |
| 1644 | Binder.restoreCallingIdentity(identity); |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | @Override |
Jae Seo | 465f0d6 | 2015-04-06 18:40:46 -0700 | [diff] [blame] | 1649 | public void timeShiftEnablePositionTracking(IBinder sessionToken, boolean enable, |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1650 | int userId) { |
| 1651 | final int callingUid = Binder.getCallingUid(); |
| 1652 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
Jae Seo | 465f0d6 | 2015-04-06 18:40:46 -0700 | [diff] [blame] | 1653 | userId, "timeShiftEnablePositionTracking"); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1654 | final long identity = Binder.clearCallingIdentity(); |
| 1655 | try { |
| 1656 | synchronized (mLock) { |
| 1657 | try { |
| 1658 | getSessionLocked(sessionToken, callingUid, resolvedUserId) |
Jae Seo | 465f0d6 | 2015-04-06 18:40:46 -0700 | [diff] [blame] | 1659 | .timeShiftEnablePositionTracking(enable); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1660 | } catch (RemoteException | SessionNotFoundException e) { |
Jae Seo | 465f0d6 | 2015-04-06 18:40:46 -0700 | [diff] [blame] | 1661 | Slog.e(TAG, "error in timeShiftEnablePositionTracking", e); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | } finally { |
| 1665 | Binder.restoreCallingIdentity(identity); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | @Override |
Dongwon Kang | 0cb5244 | 2016-04-22 11:00:03 -0700 | [diff] [blame] | 1670 | public void startRecording(IBinder sessionToken, @Nullable Uri programUri, int userId) { |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 1671 | final int callingUid = Binder.getCallingUid(); |
| 1672 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1673 | userId, "startRecording"); |
| 1674 | final long identity = Binder.clearCallingIdentity(); |
| 1675 | try { |
| 1676 | synchronized (mLock) { |
| 1677 | try { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 1678 | getSessionLocked(sessionToken, callingUid, resolvedUserId).startRecording( |
Dongwon Kang | 0cb5244 | 2016-04-22 11:00:03 -0700 | [diff] [blame] | 1679 | programUri); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 1680 | } catch (RemoteException | SessionNotFoundException e) { |
| 1681 | Slog.e(TAG, "error in startRecording", e); |
| 1682 | } |
| 1683 | } |
| 1684 | } finally { |
| 1685 | Binder.restoreCallingIdentity(identity); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | @Override |
| 1690 | public void stopRecording(IBinder sessionToken, int userId) { |
| 1691 | final int callingUid = Binder.getCallingUid(); |
| 1692 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1693 | userId, "stopRecording"); |
| 1694 | final long identity = Binder.clearCallingIdentity(); |
| 1695 | try { |
| 1696 | synchronized (mLock) { |
| 1697 | try { |
| 1698 | getSessionLocked(sessionToken, callingUid, resolvedUserId).stopRecording(); |
| 1699 | } catch (RemoteException | SessionNotFoundException e) { |
| 1700 | Slog.e(TAG, "error in stopRecording", e); |
| 1701 | } |
| 1702 | } |
| 1703 | } finally { |
| 1704 | Binder.restoreCallingIdentity(identity); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | @Override |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1709 | public List<TvInputHardwareInfo> getHardwareList() throws RemoteException { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1710 | if (mContext.checkCallingPermission(android.Manifest.permission.TV_INPUT_HARDWARE) |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1711 | != PackageManager.PERMISSION_GRANTED) { |
| 1712 | return null; |
| 1713 | } |
| 1714 | |
| 1715 | final long identity = Binder.clearCallingIdentity(); |
| 1716 | try { |
| 1717 | return mTvInputHardwareManager.getHardwareList(); |
| 1718 | } finally { |
| 1719 | Binder.restoreCallingIdentity(identity); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | @Override |
| 1724 | public ITvInputHardware acquireTvInputHardware(int deviceId, |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1725 | ITvInputHardwareCallback callback, TvInputInfo info, int userId) |
| 1726 | throws RemoteException { |
| 1727 | if (mContext.checkCallingPermission(android.Manifest.permission.TV_INPUT_HARDWARE) |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1728 | != PackageManager.PERMISSION_GRANTED) { |
| 1729 | return null; |
| 1730 | } |
| 1731 | |
| 1732 | final long identity = Binder.clearCallingIdentity(); |
| 1733 | final int callingUid = Binder.getCallingUid(); |
| 1734 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1735 | userId, "acquireTvInputHardware"); |
| 1736 | try { |
| 1737 | return mTvInputHardwareManager.acquireHardware( |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1738 | deviceId, callback, info, callingUid, resolvedUserId); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1739 | } finally { |
| 1740 | Binder.restoreCallingIdentity(identity); |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | @Override |
| 1745 | public void releaseTvInputHardware(int deviceId, ITvInputHardware hardware, int userId) |
| 1746 | throws RemoteException { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1747 | if (mContext.checkCallingPermission(android.Manifest.permission.TV_INPUT_HARDWARE) |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1748 | != PackageManager.PERMISSION_GRANTED) { |
| 1749 | return; |
| 1750 | } |
| 1751 | |
| 1752 | final long identity = Binder.clearCallingIdentity(); |
| 1753 | final int callingUid = Binder.getCallingUid(); |
| 1754 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1755 | userId, "releaseTvInputHardware"); |
| 1756 | try { |
| 1757 | mTvInputHardwareManager.releaseHardware( |
| 1758 | deviceId, hardware, callingUid, resolvedUserId); |
| 1759 | } finally { |
| 1760 | Binder.restoreCallingIdentity(identity); |
| 1761 | } |
| 1762 | } |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 1763 | |
| 1764 | @Override |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1765 | public List<DvbDeviceInfo> getDvbDeviceList() throws RemoteException { |
| 1766 | if (mContext.checkCallingPermission(android.Manifest.permission.DVB_DEVICE) |
| 1767 | != PackageManager.PERMISSION_GRANTED) { |
| 1768 | throw new SecurityException("Requires DVB_DEVICE permission"); |
| 1769 | } |
| 1770 | |
| 1771 | final long identity = Binder.clearCallingIdentity(); |
| 1772 | try { |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1773 | // Pattern1: /dev/dvb%d.frontend%d |
| 1774 | ArrayList<DvbDeviceInfo> deviceInfosFromPattern1 = new ArrayList<>(); |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1775 | File devDirectory = new File("/dev"); |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1776 | boolean dvbDirectoryFound = false; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1777 | for (String fileName : devDirectory.list()) { |
| 1778 | Matcher matcher = sFrontEndDevicePattern.matcher(fileName); |
| 1779 | if (matcher.find()) { |
| 1780 | int adapterId = Integer.parseInt(matcher.group(1)); |
| 1781 | int deviceId = Integer.parseInt(matcher.group(2)); |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1782 | deviceInfosFromPattern1.add(new DvbDeviceInfo(adapterId, deviceId)); |
| 1783 | } |
| 1784 | if (TextUtils.equals("dvb", fileName)) { |
| 1785 | dvbDirectoryFound = true; |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1786 | } |
| 1787 | } |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1788 | if (!dvbDirectoryFound) { |
| 1789 | return Collections.unmodifiableList(deviceInfosFromPattern1); |
| 1790 | } |
| 1791 | File dvbDirectory = new File(DVB_DIRECTORY); |
| 1792 | // Pattern2: /dev/dvb/adapter%d/frontend%d |
| 1793 | ArrayList<DvbDeviceInfo> deviceInfosFromPattern2 = new ArrayList<>(); |
| 1794 | for (String fileNameInDvb : dvbDirectory.list()) { |
| 1795 | Matcher adapterMatcher = sAdapterDirPattern.matcher(fileNameInDvb); |
| 1796 | if (adapterMatcher.find()) { |
| 1797 | int adapterId = Integer.parseInt(adapterMatcher.group(1)); |
| 1798 | File adapterDirectory = new File(DVB_DIRECTORY + "/" + fileNameInDvb); |
| 1799 | for (String fileNameInAdapter : adapterDirectory.list()) { |
| 1800 | Matcher frontendMatcher = sFrontEndInAdapterDirPattern.matcher( |
| 1801 | fileNameInAdapter); |
| 1802 | if (frontendMatcher.find()) { |
| 1803 | int deviceId = Integer.parseInt(frontendMatcher.group(1)); |
| 1804 | deviceInfosFromPattern2.add( |
| 1805 | new DvbDeviceInfo(adapterId, deviceId)); |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | return deviceInfosFromPattern2.isEmpty() |
| 1811 | ? Collections.unmodifiableList(deviceInfosFromPattern1) |
| 1812 | : Collections.unmodifiableList(deviceInfosFromPattern2); |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1813 | } finally { |
| 1814 | Binder.restoreCallingIdentity(identity); |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | @Override |
| 1819 | public ParcelFileDescriptor openDvbDevice(DvbDeviceInfo info, int device) |
| 1820 | throws RemoteException { |
| 1821 | if (mContext.checkCallingPermission(android.Manifest.permission.DVB_DEVICE) |
| 1822 | != PackageManager.PERMISSION_GRANTED) { |
| 1823 | throw new SecurityException("Requires DVB_DEVICE permission"); |
| 1824 | } |
| 1825 | |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1826 | File devDirectory = new File("/dev"); |
| 1827 | boolean dvbDeviceFound = false; |
| 1828 | for (String fileName : devDirectory.list()) { |
| 1829 | if (TextUtils.equals("dvb", fileName)) { |
| 1830 | File dvbDirectory = new File(DVB_DIRECTORY); |
| 1831 | for (String fileNameInDvb : dvbDirectory.list()) { |
| 1832 | Matcher adapterMatcher = sAdapterDirPattern.matcher(fileNameInDvb); |
| 1833 | if (adapterMatcher.find()) { |
| 1834 | File adapterDirectory = new File(DVB_DIRECTORY + "/" + fileNameInDvb); |
| 1835 | for (String fileNameInAdapter : adapterDirectory.list()) { |
| 1836 | Matcher frontendMatcher = sFrontEndInAdapterDirPattern.matcher( |
| 1837 | fileNameInAdapter); |
| 1838 | if (frontendMatcher.find()) { |
| 1839 | dvbDeviceFound = true; |
| 1840 | break; |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | if (dvbDeviceFound) { |
| 1845 | break; |
| 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | if (dvbDeviceFound) { |
| 1850 | break; |
| 1851 | } |
| 1852 | } |
| 1853 | |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1854 | final long identity = Binder.clearCallingIdentity(); |
| 1855 | try { |
| 1856 | String deviceFileName; |
| 1857 | switch (device) { |
| 1858 | case TvInputManager.DVB_DEVICE_DEMUX: |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1859 | deviceFileName = String.format(dvbDeviceFound |
| 1860 | ? "/dev/dvb/adapter%d/demux%d" : "/dev/dvb%d.demux%d", |
| 1861 | info.getAdapterId(), info.getDeviceId()); |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1862 | break; |
| 1863 | case TvInputManager.DVB_DEVICE_DVR: |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1864 | deviceFileName = String.format(dvbDeviceFound |
| 1865 | ? "/dev/dvb/adapter%d/dvr%d" : "/dev/dvb%d.dvr%d", |
| 1866 | info.getAdapterId(), info.getDeviceId()); |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1867 | break; |
| 1868 | case TvInputManager.DVB_DEVICE_FRONTEND: |
Jiabin | ef1659f | 2016-11-18 10:50:15 +0900 | [diff] [blame] | 1869 | deviceFileName = String.format(dvbDeviceFound |
| 1870 | ? "/dev/dvb/adapter%d/frontend%d" : "/dev/dvb%d.frontend%d", |
| 1871 | info.getAdapterId(), info.getDeviceId()); |
Jaesung Chung | 58739e7 | 2015-04-24 19:39:59 +0900 | [diff] [blame] | 1872 | break; |
| 1873 | default: |
| 1874 | throw new IllegalArgumentException("Invalid DVB device: " + device); |
| 1875 | } |
| 1876 | try { |
| 1877 | // The DVB frontend device only needs to be opened in read/write mode, which |
| 1878 | // allows performing tuning operations. The DVB demux and DVR device are enough |
| 1879 | // to be opened in read only mode. |
| 1880 | return ParcelFileDescriptor.open(new File(deviceFileName), |
| 1881 | TvInputManager.DVB_DEVICE_FRONTEND == device |
| 1882 | ? ParcelFileDescriptor.MODE_READ_WRITE |
| 1883 | : ParcelFileDescriptor.MODE_READ_ONLY); |
| 1884 | } catch (FileNotFoundException e) { |
| 1885 | return null; |
| 1886 | } |
| 1887 | } finally { |
| 1888 | Binder.restoreCallingIdentity(identity); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | @Override |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1893 | public List<TvStreamConfig> getAvailableTvStreamConfigList(String inputId, int userId) |
| 1894 | throws RemoteException { |
Shubang | dd3ec0b | 2017-06-23 13:54:10 -0700 | [diff] [blame] | 1895 | ensureCaptureTvInputPermission(); |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1896 | |
| 1897 | final long identity = Binder.clearCallingIdentity(); |
| 1898 | final int callingUid = Binder.getCallingUid(); |
| 1899 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1900 | userId, "getAvailableTvStreamConfigList"); |
| 1901 | try { |
| 1902 | return mTvInputHardwareManager.getAvailableTvStreamConfigList( |
| 1903 | inputId, callingUid, resolvedUserId); |
| 1904 | } finally { |
| 1905 | Binder.restoreCallingIdentity(identity); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | @Override |
| 1910 | public boolean captureFrame(String inputId, Surface surface, TvStreamConfig config, |
| 1911 | int userId) |
| 1912 | throws RemoteException { |
Shubang | dd3ec0b | 2017-06-23 13:54:10 -0700 | [diff] [blame] | 1913 | ensureCaptureTvInputPermission(); |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1914 | |
| 1915 | final long identity = Binder.clearCallingIdentity(); |
| 1916 | final int callingUid = Binder.getCallingUid(); |
| 1917 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1918 | userId, "captureFrame"); |
| 1919 | try { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1920 | String hardwareInputId = null; |
Terry Heo | 79124a7 | 2014-07-21 15:17:17 +0900 | [diff] [blame] | 1921 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1922 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1923 | if (userState.inputMap.get(inputId) == null) { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 1924 | Slog.e(TAG, "input not found for " + inputId); |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1925 | return false; |
| 1926 | } |
| 1927 | for (SessionState sessionState : userState.sessionStateMap.values()) { |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 1928 | if (sessionState.inputId.equals(inputId) |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1929 | && sessionState.hardwareSessionToken != null) { |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1930 | hardwareInputId = userState.sessionStateMap.get( |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 1931 | sessionState.hardwareSessionToken).inputId; |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1932 | break; |
| 1933 | } |
| 1934 | } |
Terry Heo | 79124a7 | 2014-07-21 15:17:17 +0900 | [diff] [blame] | 1935 | } |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1936 | return mTvInputHardwareManager.captureFrame( |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 1937 | (hardwareInputId != null) ? hardwareInputId : inputId, |
Terry Heo | 79124a7 | 2014-07-21 15:17:17 +0900 | [diff] [blame] | 1938 | surface, config, callingUid, resolvedUserId); |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1939 | } finally { |
| 1940 | Binder.restoreCallingIdentity(identity); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | @Override |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1945 | public boolean isSingleSessionActive(int userId) throws RemoteException { |
Shubang | dd3ec0b | 2017-06-23 13:54:10 -0700 | [diff] [blame] | 1946 | ensureCaptureTvInputPermission(); |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1947 | final long identity = Binder.clearCallingIdentity(); |
| 1948 | final int callingUid = Binder.getCallingUid(); |
| 1949 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1950 | userId, "isSingleSessionActive"); |
| 1951 | try { |
| 1952 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 1953 | UserState userState = getOrCreateUserStateLocked(resolvedUserId); |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1954 | if (userState.sessionStateMap.size() == 1) { |
| 1955 | return true; |
Jae Seo | 93ff14b | 2015-06-21 14:08:54 -0700 | [diff] [blame] | 1956 | } else if (userState.sessionStateMap.size() == 2) { |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1957 | SessionState[] sessionStates = userState.sessionStateMap.values().toArray( |
Jae Seo | 93ff14b | 2015-06-21 14:08:54 -0700 | [diff] [blame] | 1958 | new SessionState[2]); |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1959 | // Check if there is a wrapper input. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 1960 | if (sessionStates[0].hardwareSessionToken != null |
| 1961 | || sessionStates[1].hardwareSessionToken != null) { |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1962 | return true; |
| 1963 | } |
| 1964 | } |
| 1965 | return false; |
| 1966 | } |
| 1967 | } finally { |
| 1968 | Binder.restoreCallingIdentity(identity); |
| 1969 | } |
| 1970 | } |
| 1971 | |
Shubang | dd3ec0b | 2017-06-23 13:54:10 -0700 | [diff] [blame] | 1972 | private void ensureCaptureTvInputPermission() { |
| 1973 | if (mContext.checkCallingPermission( |
| 1974 | android.Manifest.permission.CAPTURE_TV_INPUT) |
| 1975 | != PackageManager.PERMISSION_GRANTED) { |
| 1976 | throw new SecurityException("Requires CAPTURE_TV_INPUT permission"); |
| 1977 | } |
| 1978 | } |
| 1979 | |
Terry Heo | df9f0a3 | 2014-08-06 13:53:33 +0900 | [diff] [blame] | 1980 | @Override |
Dongwon Kang | 2e7f5ce | 2017-04-05 18:33:50 -0700 | [diff] [blame] | 1981 | public void requestChannelBrowsable(Uri channelUri, int userId) |
| 1982 | throws RemoteException { |
| 1983 | final String callingPackageName = getCallingPackageName(); |
| 1984 | final long identity = Binder.clearCallingIdentity(); |
| 1985 | final int callingUid = Binder.getCallingUid(); |
| 1986 | final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid, |
| 1987 | userId, "requestChannelBrowsable"); |
| 1988 | try { |
| 1989 | Intent intent = new Intent(TvContract.ACTION_CHANNEL_BROWSABLE_REQUESTED); |
| 1990 | List<ResolveInfo> list = getContext().getPackageManager() |
| 1991 | .queryBroadcastReceivers(intent, 0); |
| 1992 | if (list != null) { |
| 1993 | for (ResolveInfo info : list) { |
| 1994 | String receiverPackageName = info.activityInfo.packageName; |
| 1995 | intent.putExtra(TvContract.EXTRA_CHANNEL_ID, ContentUris.parseId( |
| 1996 | channelUri)); |
| 1997 | intent.putExtra(TvContract.EXTRA_PACKAGE_NAME, callingPackageName); |
| 1998 | intent.setPackage(receiverPackageName); |
| 1999 | getContext().sendBroadcastAsUser(intent, new UserHandle(resolvedUserId)); |
| 2000 | } |
| 2001 | } |
| 2002 | } finally { |
| 2003 | Binder.restoreCallingIdentity(identity); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | @Override |
Jae Seo | 0f8fc34 | 2014-07-02 10:47:08 -0700 | [diff] [blame] | 2008 | @SuppressWarnings("resource") |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2009 | protected void dump(FileDescriptor fd, final PrintWriter writer, String[] args) { |
| 2010 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2011 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2012 | |
| 2013 | synchronized (mLock) { |
| 2014 | pw.println("User Ids (Current user: " + mCurrentUserId + "):"); |
| 2015 | pw.increaseIndent(); |
| 2016 | for (int i = 0; i < mUserStates.size(); i++) { |
| 2017 | int userId = mUserStates.keyAt(i); |
| 2018 | pw.println(Integer.valueOf(userId)); |
| 2019 | } |
| 2020 | pw.decreaseIndent(); |
| 2021 | |
| 2022 | for (int i = 0; i < mUserStates.size(); i++) { |
| 2023 | int userId = mUserStates.keyAt(i); |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2024 | UserState userState = getOrCreateUserStateLocked(userId); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2025 | pw.println("UserState (" + userId + "):"); |
| 2026 | pw.increaseIndent(); |
| 2027 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2028 | pw.println("inputMap: inputId -> TvInputState"); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2029 | pw.increaseIndent(); |
Jaewan Kim | 8e6b51b | 2014-07-15 13:01:57 +0900 | [diff] [blame] | 2030 | for (Map.Entry<String, TvInputState> entry: userState.inputMap.entrySet()) { |
| 2031 | pw.println(entry.getKey() + ": " + entry.getValue()); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2032 | } |
| 2033 | pw.decreaseIndent(); |
| 2034 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2035 | pw.println("packageSet:"); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2036 | pw.increaseIndent(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2037 | for (String packageName : userState.packageSet) { |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2038 | pw.println(packageName); |
| 2039 | } |
| 2040 | pw.decreaseIndent(); |
| 2041 | |
| 2042 | pw.println("clientStateMap: ITvInputClient -> ClientState"); |
| 2043 | pw.increaseIndent(); |
| 2044 | for (Map.Entry<IBinder, ClientState> entry : |
| 2045 | userState.clientStateMap.entrySet()) { |
| 2046 | ClientState client = entry.getValue(); |
| 2047 | pw.println(entry.getKey() + ": " + client); |
| 2048 | |
| 2049 | pw.increaseIndent(); |
| 2050 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2051 | pw.println("sessionTokens:"); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2052 | pw.increaseIndent(); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2053 | for (IBinder token : client.sessionTokens) { |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2054 | pw.println("" + token); |
| 2055 | } |
| 2056 | pw.decreaseIndent(); |
| 2057 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2058 | pw.println("clientTokens: " + client.clientToken); |
| 2059 | pw.println("userId: " + client.userId); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2060 | |
| 2061 | pw.decreaseIndent(); |
| 2062 | } |
| 2063 | pw.decreaseIndent(); |
| 2064 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2065 | pw.println("serviceStateMap: ComponentName -> ServiceState"); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2066 | pw.increaseIndent(); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2067 | for (Map.Entry<ComponentName, ServiceState> entry : |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2068 | userState.serviceStateMap.entrySet()) { |
| 2069 | ServiceState service = entry.getValue(); |
| 2070 | pw.println(entry.getKey() + ": " + service); |
| 2071 | |
| 2072 | pw.increaseIndent(); |
| 2073 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2074 | pw.println("sessionTokens:"); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2075 | pw.increaseIndent(); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2076 | for (IBinder token : service.sessionTokens) { |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2077 | pw.println("" + token); |
| 2078 | } |
| 2079 | pw.decreaseIndent(); |
| 2080 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2081 | pw.println("service: " + service.service); |
| 2082 | pw.println("callback: " + service.callback); |
| 2083 | pw.println("bound: " + service.bound); |
| 2084 | pw.println("reconnecting: " + service.reconnecting); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2085 | |
| 2086 | pw.decreaseIndent(); |
| 2087 | } |
| 2088 | pw.decreaseIndent(); |
| 2089 | |
| 2090 | pw.println("sessionStateMap: ITvInputSession -> SessionState"); |
| 2091 | pw.increaseIndent(); |
| 2092 | for (Map.Entry<IBinder, SessionState> entry : |
| 2093 | userState.sessionStateMap.entrySet()) { |
| 2094 | SessionState session = entry.getValue(); |
| 2095 | pw.println(entry.getKey() + ": " + session); |
| 2096 | |
| 2097 | pw.increaseIndent(); |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2098 | pw.println("inputId: " + session.inputId); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2099 | pw.println("client: " + session.client); |
| 2100 | pw.println("seq: " + session.seq); |
| 2101 | pw.println("callingUid: " + session.callingUid); |
| 2102 | pw.println("userId: " + session.userId); |
| 2103 | pw.println("sessionToken: " + session.sessionToken); |
| 2104 | pw.println("session: " + session.session); |
| 2105 | pw.println("logUri: " + session.logUri); |
| 2106 | pw.println("hardwareSessionToken: " + session.hardwareSessionToken); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2107 | pw.decreaseIndent(); |
| 2108 | } |
| 2109 | pw.decreaseIndent(); |
| 2110 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2111 | pw.println("callbackSet:"); |
| 2112 | pw.increaseIndent(); |
| 2113 | for (ITvInputManagerCallback callback : userState.callbackSet) { |
| 2114 | pw.println(callback.toString()); |
| 2115 | } |
| 2116 | pw.decreaseIndent(); |
| 2117 | |
Ji-Hwan Lee | 956afc2 | 2014-07-26 11:31:39 +0900 | [diff] [blame] | 2118 | pw.println("mainSessionToken: " + userState.mainSessionToken); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2119 | pw.decreaseIndent(); |
| 2120 | } |
| 2121 | } |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame] | 2122 | mTvInputHardwareManager.dump(fd, writer, args); |
Jaewan Kim | e14c3f4 | 2014-06-27 13:47:48 +0900 | [diff] [blame] | 2123 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2124 | } |
| 2125 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2126 | private static final class UserState { |
| 2127 | // A mapping from the TV input id to its TvInputState. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2128 | private Map<String, TvInputState> inputMap = new HashMap<>(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2129 | |
| 2130 | // A set of all TV input packages. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2131 | private final Set<String> packageSet = new HashSet<>(); |
Jae Seo | 5c80ad2 | 2014-06-12 19:52:58 -0700 | [diff] [blame] | 2132 | |
Jae Seo | 9c165d6 | 2014-08-25 14:39:26 -0700 | [diff] [blame] | 2133 | // A list of all TV content rating systems defined. |
| 2134 | private final List<TvContentRatingSystemInfo> |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2135 | contentRatingSystemList = new ArrayList<>(); |
Sungsoo Lim | 5c5b83f | 2014-07-29 11:48:36 +0900 | [diff] [blame] | 2136 | |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2137 | // A mapping from the token of a client to its state. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2138 | private final Map<IBinder, ClientState> clientStateMap = new HashMap<>(); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2139 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2140 | // A mapping from the name of a TV input service to its state. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2141 | private final Map<ComponentName, ServiceState> serviceStateMap = new HashMap<>(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2142 | |
| 2143 | // A mapping from the token of a TV input session to its state. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2144 | private final Map<IBinder, SessionState> sessionStateMap = new HashMap<>(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2145 | |
| 2146 | // A set of callbacks. |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2147 | private final Set<ITvInputManagerCallback> callbackSet = new HashSet<>(); |
Terry Heo | 79124a7 | 2014-07-21 15:17:17 +0900 | [diff] [blame] | 2148 | |
Ji-Hwan Lee | 4c52697 | 2014-07-22 04:46:30 +0900 | [diff] [blame] | 2149 | // The token of a "main" TV input session. |
| 2150 | private IBinder mainSessionToken = null; |
Jae Seo | 783645e | 2014-07-28 17:30:50 +0900 | [diff] [blame] | 2151 | |
| 2152 | // Persistent data store for all internal settings maintained by the TV input manager |
| 2153 | // service. |
| 2154 | private final PersistentDataStore persistentDataStore; |
| 2155 | |
| 2156 | private UserState(Context context, int userId) { |
| 2157 | persistentDataStore = new PersistentDataStore(context, userId); |
| 2158 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2159 | } |
| 2160 | |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2161 | private final class ClientState implements IBinder.DeathRecipient { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2162 | private final List<IBinder> sessionTokens = new ArrayList<>(); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2163 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2164 | private IBinder clientToken; |
| 2165 | private final int userId; |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2166 | |
| 2167 | ClientState(IBinder clientToken, int userId) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2168 | this.clientToken = clientToken; |
| 2169 | this.userId = userId; |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | public boolean isEmpty() { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2173 | return sessionTokens.isEmpty(); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | @Override |
| 2177 | public void binderDied() { |
| 2178 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2179 | UserState userState = getOrCreateUserStateLocked(userId); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2180 | // DO NOT remove the client state of clientStateMap in this method. It will be |
Ji-Hwan Lee | a65118e | 2014-07-24 16:30:02 +0900 | [diff] [blame] | 2181 | // removed in releaseSessionLocked(). |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2182 | ClientState clientState = userState.clientStateMap.get(clientToken); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2183 | if (clientState != null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2184 | while (clientState.sessionTokens.size() > 0) { |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2185 | releaseSessionLocked( |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2186 | clientState.sessionTokens.get(0), Process.SYSTEM_UID, userId); |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2187 | } |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2188 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2189 | clientToken = null; |
Sungsoo Lim | 72ad7bf | 2014-05-14 09:21:08 +0900 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | } |
| 2193 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2194 | private final class ServiceState { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2195 | private final List<IBinder> sessionTokens = new ArrayList<>(); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2196 | private final ServiceConnection connection; |
| 2197 | private final ComponentName component; |
| 2198 | private final boolean isHardware; |
Shubang | 71d5c76 | 2017-02-23 15:46:40 -0800 | [diff] [blame] | 2199 | private final Map<String, TvInputInfo> hardwareInputMap = new HashMap<>(); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2200 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2201 | private ITvInputService service; |
| 2202 | private ServiceCallback callback; |
| 2203 | private boolean bound; |
| 2204 | private boolean reconnecting; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2205 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2206 | private ServiceState(ComponentName component, int userId) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2207 | this.component = component; |
| 2208 | this.connection = new InputServiceConnection(component, userId); |
| 2209 | this.isHardware = hasHardwarePermission(mContext.getPackageManager(), component); |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | private static final class TvInputState { |
| 2214 | // A TvInputInfo object which represents the TV input. |
| 2215 | private TvInputInfo info; |
| 2216 | |
| 2217 | // The state of TV input. Connected by default. |
| 2218 | private int state = INPUT_STATE_CONNECTED; |
| 2219 | |
| 2220 | @Override |
| 2221 | public String toString() { |
| 2222 | return "info: " + info + "; state: " + state; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2226 | private final class SessionState implements IBinder.DeathRecipient { |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2227 | private final String inputId; |
| 2228 | private final ComponentName componentName; |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2229 | private final boolean isRecordingSession; |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2230 | private final ITvInputClient client; |
| 2231 | private final int seq; |
| 2232 | private final int callingUid; |
| 2233 | private final int userId; |
| 2234 | private final IBinder sessionToken; |
| 2235 | private ITvInputSession session; |
| 2236 | private Uri logUri; |
Dongwon Kang | bd2fa2c | 2014-07-25 19:52:08 +0900 | [diff] [blame] | 2237 | // Not null if this session represents an external device connected to a hardware TV input. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2238 | private IBinder hardwareSessionToken; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2239 | |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2240 | private SessionState(IBinder sessionToken, String inputId, ComponentName componentName, |
| 2241 | boolean isRecordingSession, ITvInputClient client, int seq, int callingUid, |
| 2242 | int userId) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2243 | this.sessionToken = sessionToken; |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2244 | this.inputId = inputId; |
| 2245 | this.componentName = componentName; |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2246 | this.isRecordingSession = isRecordingSession; |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2247 | this.client = client; |
| 2248 | this.seq = seq; |
| 2249 | this.callingUid = callingUid; |
| 2250 | this.userId = userId; |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | @Override |
| 2254 | public void binderDied() { |
| 2255 | synchronized (mLock) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2256 | session = null; |
shubang | 8049f20 | 2016-04-25 11:21:42 -0700 | [diff] [blame] | 2257 | clearSessionAndNotifyClientLocked(this); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2258 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | private final class InputServiceConnection implements ServiceConnection { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2263 | private final ComponentName mComponent; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2264 | private final int mUserId; |
| 2265 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2266 | private InputServiceConnection(ComponentName component, int userId) { |
| 2267 | mComponent = component; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2268 | mUserId = userId; |
| 2269 | } |
| 2270 | |
| 2271 | @Override |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2272 | public void onServiceConnected(ComponentName component, IBinder service) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2273 | if (DEBUG) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2274 | Slog.d(TAG, "onServiceConnected(component=" + component + ")"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2275 | } |
| 2276 | synchronized (mLock) { |
Dongwon Kang | 81e3c3e | 2015-09-11 15:24:25 -0700 | [diff] [blame] | 2277 | UserState userState = mUserStates.get(mUserId); |
| 2278 | if (userState == null) { |
| 2279 | // The user was removed while connecting. |
| 2280 | mContext.unbindService(this); |
| 2281 | return; |
| 2282 | } |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2283 | ServiceState serviceState = userState.serviceStateMap.get(mComponent); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2284 | serviceState.service = ITvInputService.Stub.asInterface(service); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2285 | |
| 2286 | // Register a callback, if we need to. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2287 | if (serviceState.isHardware && serviceState.callback == null) { |
| 2288 | serviceState.callback = new ServiceCallback(mComponent, mUserId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2289 | try { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2290 | serviceState.service.registerCallback(serviceState.callback); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2291 | } catch (RemoteException e) { |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 2292 | Slog.e(TAG, "error in registerCallback", e); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 2296 | List<IBinder> tokensToBeRemoved = new ArrayList<>(); |
| 2297 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2298 | // And create sessions, if any. |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2299 | for (IBinder sessionToken : serviceState.sessionTokens) { |
shubang | af2fb66 | 2018-11-09 17:03:46 -0800 | [diff] [blame] | 2300 | if (!createSessionInternalLocked(serviceState.service, sessionToken, mUserId)) { |
| 2301 | tokensToBeRemoved.add(sessionToken); |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | for (IBinder sessionToken : tokensToBeRemoved) { |
| 2306 | removeSessionStateLocked(sessionToken, mUserId); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2307 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2308 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2309 | for (TvInputState inputState : userState.inputMap.values()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2310 | if (inputState.info.getComponent().equals(component) |
Jae Seo | 82fce64 | 2015-04-20 15:37:50 -0700 | [diff] [blame] | 2311 | && inputState.state != INPUT_STATE_CONNECTED) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2312 | notifyInputStateChangedLocked(userState, inputState.info.getId(), |
| 2313 | inputState.state, null); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2317 | if (serviceState.isHardware) { |
Shubang | 71d5c76 | 2017-02-23 15:46:40 -0800 | [diff] [blame] | 2318 | serviceState.hardwareInputMap.clear(); |
Jae Seo | c980f43d | 2016-02-09 23:46:58 -0800 | [diff] [blame] | 2319 | for (TvInputHardwareInfo hardware : mTvInputHardwareManager.getHardwareList()) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2320 | try { |
Jae Seo | c980f43d | 2016-02-09 23:46:58 -0800 | [diff] [blame] | 2321 | serviceState.service.notifyHardwareAdded(hardware); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2322 | } catch (RemoteException e) { |
| 2323 | Slog.e(TAG, "error in notifyHardwareAdded", e); |
| 2324 | } |
| 2325 | } |
Jae Seo | c980f43d | 2016-02-09 23:46:58 -0800 | [diff] [blame] | 2326 | for (HdmiDeviceInfo device : mTvInputHardwareManager.getHdmiDeviceList()) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2327 | try { |
Jae Seo | c980f43d | 2016-02-09 23:46:58 -0800 | [diff] [blame] | 2328 | serviceState.service.notifyHdmiDeviceAdded(device); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2329 | } catch (RemoteException e) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 2330 | Slog.e(TAG, "error in notifyHdmiDeviceAdded", e); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2331 | } |
| 2332 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2333 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | @Override |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2338 | public void onServiceDisconnected(ComponentName component) { |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2339 | if (DEBUG) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2340 | Slog.d(TAG, "onServiceDisconnected(component=" + component + ")"); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2341 | } |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2342 | if (!mComponent.equals(component)) { |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2343 | throw new IllegalArgumentException("Mismatched ComponentName: " |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2344 | + mComponent + " (expected), " + component + " (actual)."); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2345 | } |
| 2346 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2347 | UserState userState = getOrCreateUserStateLocked(mUserId); |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2348 | ServiceState serviceState = userState.serviceStateMap.get(mComponent); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2349 | if (serviceState != null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2350 | serviceState.reconnecting = true; |
| 2351 | serviceState.bound = false; |
| 2352 | serviceState.service = null; |
| 2353 | serviceState.callback = null; |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2354 | |
Dongwon Kang | 426c9a4 | 2014-08-26 17:39:21 +0900 | [diff] [blame] | 2355 | abortPendingCreateSessionRequestsLocked(serviceState, null, mUserId); |
Sungsoo Lim | 2b35a72 | 2014-04-17 17:09:15 +0900 | [diff] [blame] | 2356 | } |
| 2357 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | private final class ServiceCallback extends ITvInputServiceCallback.Stub { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2362 | private final ComponentName mComponent; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2363 | private final int mUserId; |
| 2364 | |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2365 | ServiceCallback(ComponentName component, int userId) { |
| 2366 | mComponent = component; |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2367 | mUserId = userId; |
| 2368 | } |
| 2369 | |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2370 | private void ensureHardwarePermission() { |
| 2371 | if (mContext.checkCallingPermission(android.Manifest.permission.TV_INPUT_HARDWARE) |
| 2372 | != PackageManager.PERMISSION_GRANTED) { |
| 2373 | throw new SecurityException("The caller does not have hardware permission"); |
| 2374 | } |
| 2375 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2376 | |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2377 | private void ensureValidInput(TvInputInfo inputInfo) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2378 | if (inputInfo.getId() == null || !mComponent.equals(inputInfo.getComponent())) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2379 | throw new IllegalArgumentException("Invalid TvInputInfo"); |
| 2380 | } |
| 2381 | } |
| 2382 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2383 | private void addHardwareInputLocked(TvInputInfo inputInfo) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2384 | ServiceState serviceState = getServiceStateLocked(mComponent, mUserId); |
Shubang | 71d5c76 | 2017-02-23 15:46:40 -0800 | [diff] [blame] | 2385 | serviceState.hardwareInputMap.put(inputInfo.getId(), inputInfo); |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 2386 | buildTvInputListLocked(mUserId, null); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2387 | } |
| 2388 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2389 | public void addHardwareInput(int deviceId, TvInputInfo inputInfo) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2390 | ensureHardwarePermission(); |
| 2391 | ensureValidInput(inputInfo); |
| 2392 | synchronized (mLock) { |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2393 | mTvInputHardwareManager.addHardwareInput(deviceId, inputInfo); |
| 2394 | addHardwareInputLocked(inputInfo); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2395 | } |
| 2396 | } |
| 2397 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2398 | public void addHdmiInput(int id, TvInputInfo inputInfo) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2399 | ensureHardwarePermission(); |
| 2400 | ensureValidInput(inputInfo); |
| 2401 | synchronized (mLock) { |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2402 | mTvInputHardwareManager.addHdmiInput(id, inputInfo); |
| 2403 | addHardwareInputLocked(inputInfo); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2407 | public void removeHardwareInput(String inputId) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2408 | ensureHardwarePermission(); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2409 | synchronized (mLock) { |
Ji-Hwan Lee | 9e8ade2 | 2014-07-25 20:20:38 +0900 | [diff] [blame] | 2410 | ServiceState serviceState = getServiceStateLocked(mComponent, mUserId); |
Shubang | 71d5c76 | 2017-02-23 15:46:40 -0800 | [diff] [blame] | 2411 | boolean removed = serviceState.hardwareInputMap.remove(inputId) != null; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2412 | if (removed) { |
Chulwoo Lee | 19ba61a | 2014-09-03 00:59:35 +0900 | [diff] [blame] | 2413 | buildTvInputListLocked(mUserId, null); |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 2414 | mTvInputHardwareManager.removeHardwareInput(inputId); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2415 | } else { |
Jae Seo | fea8dd4 | 2014-08-26 13:57:41 -0700 | [diff] [blame] | 2416 | Slog.e(TAG, "failed to remove input " + inputId); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2417 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | } |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2421 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2422 | private final class SessionCallback extends ITvInputSessionCallback.Stub { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2423 | private final SessionState mSessionState; |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2424 | private final InputChannel[] mChannels; |
| 2425 | |
| 2426 | SessionCallback(SessionState sessionState, InputChannel[] channels) { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2427 | mSessionState = sessionState; |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2428 | mChannels = channels; |
| 2429 | } |
| 2430 | |
| 2431 | @Override |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2432 | public void onSessionCreated(ITvInputSession session, IBinder hardwareSessionToken) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2433 | if (DEBUG) { |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2434 | Slog.d(TAG, "onSessionCreated(inputId=" + mSessionState.inputId + ")"); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2435 | } |
| 2436 | synchronized (mLock) { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2437 | mSessionState.session = session; |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2438 | mSessionState.hardwareSessionToken = hardwareSessionToken; |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2439 | if (session != null && addSessionTokenToClientStateLocked(session)) { |
| 2440 | sendSessionTokenToClientLocked(mSessionState.client, |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2441 | mSessionState.inputId, mSessionState.sessionToken, mChannels[0], |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2442 | mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2443 | } else { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2444 | removeSessionStateLocked(mSessionState.sessionToken, mSessionState.userId); |
| 2445 | sendSessionTokenToClientLocked(mSessionState.client, |
Jae Seo | 2cdb05e | 2016-02-04 22:17:13 +0900 | [diff] [blame] | 2446 | mSessionState.inputId, null, null, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2447 | } |
| 2448 | mChannels[0].dispose(); |
| 2449 | } |
| 2450 | } |
| 2451 | |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2452 | private boolean addSessionTokenToClientStateLocked(ITvInputSession session) { |
| 2453 | try { |
| 2454 | session.asBinder().linkToDeath(mSessionState, 0); |
| 2455 | } catch (RemoteException e) { |
| 2456 | Slog.e(TAG, "session process has already died", e); |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | IBinder clientToken = mSessionState.client.asBinder(); |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2461 | UserState userState = getOrCreateUserStateLocked(mSessionState.userId); |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2462 | ClientState clientState = userState.clientStateMap.get(clientToken); |
| 2463 | if (clientState == null) { |
| 2464 | clientState = new ClientState(clientToken, mSessionState.userId); |
| 2465 | try { |
| 2466 | clientToken.linkToDeath(clientState, 0); |
| 2467 | } catch (RemoteException e) { |
| 2468 | Slog.e(TAG, "client process has already died", e); |
| 2469 | return false; |
| 2470 | } |
| 2471 | userState.clientStateMap.put(clientToken, clientState); |
| 2472 | } |
| 2473 | clientState.sessionTokens.add(mSessionState.sessionToken); |
| 2474 | return true; |
| 2475 | } |
| 2476 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2477 | @Override |
| 2478 | public void onChannelRetuned(Uri channelUri) { |
| 2479 | synchronized (mLock) { |
| 2480 | if (DEBUG) { |
| 2481 | Slog.d(TAG, "onChannelRetuned(" + channelUri + ")"); |
| 2482 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2483 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2484 | return; |
| 2485 | } |
| 2486 | try { |
| 2487 | // TODO: Consider adding this channel change in the watch log. When we do |
| 2488 | // that, how we can protect the watch log from malicious tv inputs should |
| 2489 | // be addressed. e.g. add a field which represents where the channel change |
| 2490 | // originated from. |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2491 | mSessionState.client.onChannelRetuned(channelUri, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2492 | } catch (RemoteException e) { |
| 2493 | Slog.e(TAG, "error in onChannelRetuned", e); |
| 2494 | } |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | @Override |
| 2499 | public void onTracksChanged(List<TvTrackInfo> tracks) { |
| 2500 | synchronized (mLock) { |
| 2501 | if (DEBUG) { |
| 2502 | Slog.d(TAG, "onTracksChanged(" + tracks + ")"); |
| 2503 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2504 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2505 | return; |
| 2506 | } |
| 2507 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2508 | mSessionState.client.onTracksChanged(tracks, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2509 | } catch (RemoteException e) { |
| 2510 | Slog.e(TAG, "error in onTracksChanged", e); |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | @Override |
| 2516 | public void onTrackSelected(int type, String trackId) { |
| 2517 | synchronized (mLock) { |
| 2518 | if (DEBUG) { |
| 2519 | Slog.d(TAG, "onTrackSelected(type=" + type + ", trackId=" + trackId + ")"); |
| 2520 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2521 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2522 | return; |
| 2523 | } |
| 2524 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2525 | mSessionState.client.onTrackSelected(type, trackId, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2526 | } catch (RemoteException e) { |
| 2527 | Slog.e(TAG, "error in onTrackSelected", e); |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | @Override |
| 2533 | public void onVideoAvailable() { |
| 2534 | synchronized (mLock) { |
| 2535 | if (DEBUG) { |
| 2536 | Slog.d(TAG, "onVideoAvailable()"); |
| 2537 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2538 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2539 | return; |
| 2540 | } |
| 2541 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2542 | mSessionState.client.onVideoAvailable(mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2543 | } catch (RemoteException e) { |
| 2544 | Slog.e(TAG, "error in onVideoAvailable", e); |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | @Override |
| 2550 | public void onVideoUnavailable(int reason) { |
| 2551 | synchronized (mLock) { |
| 2552 | if (DEBUG) { |
| 2553 | Slog.d(TAG, "onVideoUnavailable(" + reason + ")"); |
| 2554 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2555 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2556 | return; |
| 2557 | } |
| 2558 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2559 | mSessionState.client.onVideoUnavailable(reason, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2560 | } catch (RemoteException e) { |
| 2561 | Slog.e(TAG, "error in onVideoUnavailable", e); |
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | @Override |
| 2567 | public void onContentAllowed() { |
| 2568 | synchronized (mLock) { |
| 2569 | if (DEBUG) { |
| 2570 | Slog.d(TAG, "onContentAllowed()"); |
| 2571 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2572 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2573 | return; |
| 2574 | } |
| 2575 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2576 | mSessionState.client.onContentAllowed(mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2577 | } catch (RemoteException e) { |
| 2578 | Slog.e(TAG, "error in onContentAllowed", e); |
| 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | @Override |
| 2584 | public void onContentBlocked(String rating) { |
| 2585 | synchronized (mLock) { |
| 2586 | if (DEBUG) { |
| 2587 | Slog.d(TAG, "onContentBlocked()"); |
| 2588 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2589 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2590 | return; |
| 2591 | } |
| 2592 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2593 | mSessionState.client.onContentBlocked(rating, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2594 | } catch (RemoteException e) { |
| 2595 | Slog.e(TAG, "error in onContentBlocked", e); |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | @Override |
| 2601 | public void onLayoutSurface(int left, int top, int right, int bottom) { |
| 2602 | synchronized (mLock) { |
| 2603 | if (DEBUG) { |
| 2604 | Slog.d(TAG, "onLayoutSurface (left=" + left + ", top=" + top |
| 2605 | + ", right=" + right + ", bottom=" + bottom + ",)"); |
| 2606 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2607 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2608 | return; |
| 2609 | } |
| 2610 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2611 | mSessionState.client.onLayoutSurface(left, top, right, bottom, |
| 2612 | mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2613 | } catch (RemoteException e) { |
| 2614 | Slog.e(TAG, "error in onLayoutSurface", e); |
| 2615 | } |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | @Override |
| 2620 | public void onSessionEvent(String eventType, Bundle eventArgs) { |
| 2621 | synchronized (mLock) { |
| 2622 | if (DEBUG) { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 2623 | Slog.d(TAG, "onEvent(eventType=" + eventType + ", eventArgs=" + eventArgs |
| 2624 | + ")"); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2625 | } |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2626 | if (mSessionState.session == null || mSessionState.client == null) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2627 | return; |
| 2628 | } |
| 2629 | try { |
Ji-Hwan Lee | 9c6b5b7 | 2014-09-17 00:22:39 +0900 | [diff] [blame] | 2630 | mSessionState.client.onSessionEvent(eventType, eventArgs, mSessionState.seq); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2631 | } catch (RemoteException e) { |
| 2632 | Slog.e(TAG, "error in onSessionEvent", e); |
| 2633 | } |
| 2634 | } |
| 2635 | } |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 2636 | |
| 2637 | @Override |
| 2638 | public void onTimeShiftStatusChanged(int status) { |
| 2639 | synchronized (mLock) { |
| 2640 | if (DEBUG) { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 2641 | Slog.d(TAG, "onTimeShiftStatusChanged(status=" + status + ")"); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 2642 | } |
| 2643 | if (mSessionState.session == null || mSessionState.client == null) { |
| 2644 | return; |
| 2645 | } |
| 2646 | try { |
| 2647 | mSessionState.client.onTimeShiftStatusChanged(status, mSessionState.seq); |
| 2648 | } catch (RemoteException e) { |
| 2649 | Slog.e(TAG, "error in onTimeShiftStatusChanged", e); |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | @Override |
| 2655 | public void onTimeShiftStartPositionChanged(long timeMs) { |
| 2656 | synchronized (mLock) { |
| 2657 | if (DEBUG) { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 2658 | Slog.d(TAG, "onTimeShiftStartPositionChanged(timeMs=" + timeMs + ")"); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | if (mSessionState.session == null || mSessionState.client == null) { |
| 2661 | return; |
| 2662 | } |
| 2663 | try { |
| 2664 | mSessionState.client.onTimeShiftStartPositionChanged(timeMs, mSessionState.seq); |
| 2665 | } catch (RemoteException e) { |
| 2666 | Slog.e(TAG, "error in onTimeShiftStartPositionChanged", e); |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | @Override |
| 2672 | public void onTimeShiftCurrentPositionChanged(long timeMs) { |
| 2673 | synchronized (mLock) { |
| 2674 | if (DEBUG) { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 2675 | Slog.d(TAG, "onTimeShiftCurrentPositionChanged(timeMs=" + timeMs + ")"); |
Dongwon Kang | 6f0240c | 2015-03-31 17:56:36 -0700 | [diff] [blame] | 2676 | } |
| 2677 | if (mSessionState.session == null || mSessionState.client == null) { |
| 2678 | return; |
| 2679 | } |
| 2680 | try { |
| 2681 | mSessionState.client.onTimeShiftCurrentPositionChanged(timeMs, |
| 2682 | mSessionState.seq); |
| 2683 | } catch (RemoteException e) { |
| 2684 | Slog.e(TAG, "error in onTimeShiftCurrentPositionChanged", e); |
| 2685 | } |
| 2686 | } |
| 2687 | } |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2688 | |
| 2689 | // For the recording session only |
| 2690 | @Override |
Dongwon Kang | b55c751 | 2016-03-01 09:36:07 -0800 | [diff] [blame] | 2691 | public void onTuned(Uri channelUri) { |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2692 | synchronized (mLock) { |
| 2693 | if (DEBUG) { |
Jae Seo | e3c11e8 | 2016-02-08 23:18:49 -0800 | [diff] [blame] | 2694 | Slog.d(TAG, "onTuned()"); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2695 | } |
| 2696 | if (mSessionState.session == null || mSessionState.client == null) { |
| 2697 | return; |
| 2698 | } |
| 2699 | try { |
Dongwon Kang | b55c751 | 2016-03-01 09:36:07 -0800 | [diff] [blame] | 2700 | mSessionState.client.onTuned(mSessionState.seq, channelUri); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2701 | } catch (RemoteException e) { |
Jae Seo | e3c11e8 | 2016-02-08 23:18:49 -0800 | [diff] [blame] | 2702 | Slog.e(TAG, "error in onTuned", e); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2703 | } |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | // For the recording session only |
| 2708 | @Override |
| 2709 | public void onRecordingStopped(Uri recordedProgramUri) { |
| 2710 | synchronized (mLock) { |
| 2711 | if (DEBUG) { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 2712 | Slog.d(TAG, "onRecordingStopped(recordedProgramUri=" + recordedProgramUri |
| 2713 | + ")"); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2714 | } |
| 2715 | if (mSessionState.session == null || mSessionState.client == null) { |
| 2716 | return; |
| 2717 | } |
| 2718 | try { |
| 2719 | mSessionState.client.onRecordingStopped(recordedProgramUri, mSessionState.seq); |
| 2720 | } catch (RemoteException e) { |
| 2721 | Slog.e(TAG, "error in onRecordingStopped", e); |
| 2722 | } |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | // For the recording session only |
| 2727 | @Override |
| 2728 | public void onError(int error) { |
| 2729 | synchronized (mLock) { |
| 2730 | if (DEBUG) { |
Jae Seo | 4eee6a7 | 2016-02-06 11:11:35 +0900 | [diff] [blame] | 2731 | Slog.d(TAG, "onError(error=" + error + ")"); |
Jae Seo | a826d01 | 2016-01-18 13:03:35 -0800 | [diff] [blame] | 2732 | } |
| 2733 | if (mSessionState.session == null || mSessionState.client == null) { |
| 2734 | return; |
| 2735 | } |
| 2736 | try { |
| 2737 | mSessionState.client.onError(error, mSessionState.seq); |
| 2738 | } catch (RemoteException e) { |
| 2739 | Slog.e(TAG, "error in onError", e); |
| 2740 | } |
| 2741 | } |
| 2742 | } |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | private static final class WatchLogHandler extends Handler { |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2746 | // There are only two kinds of watch events that can happen on the system: |
| 2747 | // 1. The current TV input session is tuned to a new channel. |
| 2748 | // 2. The session is released for some reason. |
| 2749 | // The former indicates the end of the previous log entry, if any, followed by the start of |
| 2750 | // a new entry. The latter indicates the end of the most recent entry for the given session. |
| 2751 | // Here the system supplies the database the smallest set of information only that is |
| 2752 | // sufficient to consolidate the log entries while minimizing database operations in the |
| 2753 | // system service. |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 2754 | static final int MSG_LOG_WATCH_START = 1; |
| 2755 | static final int MSG_LOG_WATCH_END = 2; |
| 2756 | static final int MSG_SWITCH_CONTENT_RESOLVER = 3; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2757 | |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 2758 | private ContentResolver mContentResolver; |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2759 | |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 2760 | WatchLogHandler(ContentResolver contentResolver, Looper looper) { |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2761 | super(looper); |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2762 | mContentResolver = contentResolver; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | @Override |
| 2766 | public void handleMessage(Message msg) { |
| 2767 | switch (msg.what) { |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2768 | case MSG_LOG_WATCH_START: { |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2769 | SomeArgs args = (SomeArgs) msg.obj; |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2770 | String packageName = (String) args.arg1; |
| 2771 | long watchStartTime = (long) args.arg2; |
| 2772 | long channelId = (long) args.arg3; |
| 2773 | Bundle tuneParams = (Bundle) args.arg4; |
| 2774 | IBinder sessionToken = (IBinder) args.arg5; |
| 2775 | |
| 2776 | ContentValues values = new ContentValues(); |
| 2777 | values.put(TvContract.WatchedPrograms.COLUMN_PACKAGE_NAME, packageName); |
| 2778 | values.put(TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS, |
| 2779 | watchStartTime); |
| 2780 | values.put(TvContract.WatchedPrograms.COLUMN_CHANNEL_ID, channelId); |
| 2781 | if (tuneParams != null) { |
| 2782 | values.put(TvContract.WatchedPrograms.COLUMN_INTERNAL_TUNE_PARAMS, |
| 2783 | encodeTuneParams(tuneParams)); |
| 2784 | } |
| 2785 | values.put(TvContract.WatchedPrograms.COLUMN_INTERNAL_SESSION_TOKEN, |
| 2786 | sessionToken.toString()); |
| 2787 | |
| 2788 | mContentResolver.insert(TvContract.WatchedPrograms.CONTENT_URI, values); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2789 | args.recycle(); |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 2790 | break; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2791 | } |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2792 | case MSG_LOG_WATCH_END: { |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2793 | SomeArgs args = (SomeArgs) msg.obj; |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2794 | IBinder sessionToken = (IBinder) args.arg1; |
| 2795 | long watchEndTime = (long) args.arg2; |
| 2796 | |
| 2797 | ContentValues values = new ContentValues(); |
| 2798 | values.put(TvContract.WatchedPrograms.COLUMN_WATCH_END_TIME_UTC_MILLIS, |
| 2799 | watchEndTime); |
| 2800 | values.put(TvContract.WatchedPrograms.COLUMN_INTERNAL_SESSION_TOKEN, |
| 2801 | sessionToken.toString()); |
| 2802 | |
| 2803 | mContentResolver.insert(TvContract.WatchedPrograms.CONTENT_URI, values); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2804 | args.recycle(); |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 2805 | break; |
| 2806 | } |
| 2807 | case MSG_SWITCH_CONTENT_RESOLVER: { |
| 2808 | mContentResolver = (ContentResolver) msg.obj; |
| 2809 | break; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2810 | } |
| 2811 | default: { |
Jae Seo | 8c375fe | 2015-06-23 20:33:25 -0700 | [diff] [blame] | 2812 | Slog.w(TAG, "unhandled message code: " + msg.what); |
| 2813 | break; |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | } |
| 2817 | |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2818 | private String encodeTuneParams(Bundle tuneParams) { |
| 2819 | StringBuilder builder = new StringBuilder(); |
| 2820 | Set<String> keySet = tuneParams.keySet(); |
| 2821 | Iterator<String> it = keySet.iterator(); |
| 2822 | while (it.hasNext()) { |
| 2823 | String key = it.next(); |
| 2824 | Object value = tuneParams.get(key); |
| 2825 | if (value == null) { |
| 2826 | continue; |
Jae Seo | 579befe | 2014-08-06 19:18:37 -0700 | [diff] [blame] | 2827 | } |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2828 | builder.append(replaceEscapeCharacters(key)); |
| 2829 | builder.append("="); |
| 2830 | builder.append(replaceEscapeCharacters(value.toString())); |
| 2831 | if (it.hasNext()) { |
| 2832 | builder.append(", "); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2833 | } |
| 2834 | } |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2835 | return builder.toString(); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2838 | private String replaceEscapeCharacters(String src) { |
| 2839 | final char ESCAPE_CHARACTER = '%'; |
| 2840 | final String ENCODING_TARGET_CHARACTERS = "%=,"; |
| 2841 | StringBuilder builder = new StringBuilder(); |
| 2842 | for (char ch : src.toCharArray()) { |
| 2843 | if (ENCODING_TARGET_CHARACTERS.indexOf(ch) >= 0) { |
| 2844 | builder.append(ESCAPE_CHARACTER); |
Chulwoo Lee | 8d4ded0 | 2014-07-10 03:56:39 +0900 | [diff] [blame] | 2845 | } |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2846 | builder.append(ch); |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2847 | } |
Jae Seo | 7eb75df | 2014-08-08 22:20:48 -0700 | [diff] [blame] | 2848 | return builder.toString(); |
Jae Seo | 579befe | 2014-08-06 19:18:37 -0700 | [diff] [blame] | 2849 | } |
Jae Seo | 31dc634be | 2014-04-15 17:40:23 -0700 | [diff] [blame] | 2850 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2851 | |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2852 | private final class HardwareListener implements TvInputHardwareManager.Listener { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2853 | @Override |
| 2854 | public void onStateChanged(String inputId, int state) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2855 | synchronized (mLock) { |
| 2856 | setStateLocked(inputId, state, mCurrentUserId); |
| 2857 | } |
| 2858 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2859 | |
| 2860 | @Override |
| 2861 | public void onHardwareDeviceAdded(TvInputHardwareInfo info) { |
| 2862 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2863 | UserState userState = getOrCreateUserStateLocked(mCurrentUserId); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2864 | // Broadcast the event to all hardware inputs. |
| 2865 | for (ServiceState serviceState : userState.serviceStateMap.values()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2866 | if (!serviceState.isHardware || serviceState.service == null) continue; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2867 | try { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2868 | serviceState.service.notifyHardwareAdded(info); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2869 | } catch (RemoteException e) { |
| 2870 | Slog.e(TAG, "error in notifyHardwareAdded", e); |
| 2871 | } |
| 2872 | } |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | @Override |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2877 | public void onHardwareDeviceRemoved(TvInputHardwareInfo info) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2878 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2879 | UserState userState = getOrCreateUserStateLocked(mCurrentUserId); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2880 | // Broadcast the event to all hardware inputs. |
| 2881 | for (ServiceState serviceState : userState.serviceStateMap.values()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2882 | if (!serviceState.isHardware || serviceState.service == null) continue; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2883 | try { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2884 | serviceState.service.notifyHardwareRemoved(info); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2885 | } catch (RemoteException e) { |
| 2886 | Slog.e(TAG, "error in notifyHardwareRemoved", e); |
| 2887 | } |
| 2888 | } |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | @Override |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 2893 | public void onHdmiDeviceAdded(HdmiDeviceInfo deviceInfo) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2894 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2895 | UserState userState = getOrCreateUserStateLocked(mCurrentUserId); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2896 | // Broadcast the event to all hardware inputs. |
| 2897 | for (ServiceState serviceState : userState.serviceStateMap.values()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2898 | if (!serviceState.isHardware || serviceState.service == null) continue; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2899 | try { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2900 | serviceState.service.notifyHdmiDeviceAdded(deviceInfo); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2901 | } catch (RemoteException e) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 2902 | Slog.e(TAG, "error in notifyHdmiDeviceAdded", e); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2903 | } |
| 2904 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2905 | } |
| 2906 | } |
| 2907 | |
| 2908 | @Override |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 2909 | public void onHdmiDeviceRemoved(HdmiDeviceInfo deviceInfo) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2910 | synchronized (mLock) { |
Jae Seo | 4f1a6d4 | 2015-07-20 16:15:01 -0700 | [diff] [blame] | 2911 | UserState userState = getOrCreateUserStateLocked(mCurrentUserId); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2912 | // Broadcast the event to all hardware inputs. |
| 2913 | for (ServiceState serviceState : userState.serviceStateMap.values()) { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2914 | if (!serviceState.isHardware || serviceState.service == null) continue; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2915 | try { |
Dongwon Kang | fd8aa02 | 2014-08-28 14:59:20 +0900 | [diff] [blame] | 2916 | serviceState.service.notifyHdmiDeviceRemoved(deviceInfo); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2917 | } catch (RemoteException e) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 2918 | Slog.e(TAG, "error in notifyHdmiDeviceRemoved", e); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 2919 | } |
| 2920 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 2921 | } |
| 2922 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 2923 | |
| 2924 | @Override |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 2925 | public void onHdmiDeviceUpdated(String inputId, HdmiDeviceInfo deviceInfo) { |
| 2926 | synchronized (mLock) { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2927 | Integer state; |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 2928 | switch (deviceInfo.getDevicePowerStatus()) { |
| 2929 | case HdmiControlManager.POWER_STATUS_ON: |
| 2930 | state = INPUT_STATE_CONNECTED; |
| 2931 | break; |
| 2932 | case HdmiControlManager.POWER_STATUS_STANDBY: |
| 2933 | case HdmiControlManager.POWER_STATUS_TRANSIENT_TO_ON: |
| 2934 | case HdmiControlManager.POWER_STATUS_TRANSIENT_TO_STANDBY: |
| 2935 | state = INPUT_STATE_CONNECTED_STANDBY; |
| 2936 | break; |
| 2937 | case HdmiControlManager.POWER_STATUS_UNKNOWN: |
| 2938 | default: |
| 2939 | state = null; |
| 2940 | break; |
| 2941 | } |
| 2942 | if (state != null) { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 2943 | setStateLocked(inputId, state, mCurrentUserId); |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 2944 | } |
| 2945 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 2946 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 2947 | } |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 2948 | |
| 2949 | private static class SessionNotFoundException extends IllegalArgumentException { |
Dongwon Kang | fdce9e5 | 2014-12-04 18:08:00 +0900 | [diff] [blame] | 2950 | public SessionNotFoundException(String name) { |
| 2951 | super(name); |
| 2952 | } |
| 2953 | } |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 2954 | } |