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