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