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