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