1 /* 2 * Copyright (C) 2015 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.inputmethod.latin.permissions; 18 19 import android.app.Activity; 20 import android.content.Context; 21 22 import java.util.HashMap; 23 import java.util.List; 24 import java.util.Map; 25 26 import javax.annotation.Nonnull; 27 import javax.annotation.Nullable; 28 29 /** 30 * Manager to perform permission related tasks. Always call on the UI thread. 31 */ 32 public class PermissionsManager { 33 34 public interface PermissionsResultCallback { onRequestPermissionsResult(boolean allGranted)35 void onRequestPermissionsResult(boolean allGranted); 36 } 37 38 private int mRequestCodeId; 39 40 private final Context mContext; 41 private final Map<Integer, PermissionsResultCallback> mRequestIdToCallback = new HashMap<>(); 42 43 private static PermissionsManager sInstance; 44 PermissionsManager(Context context)45 public PermissionsManager(Context context) { 46 mContext = context; 47 } 48 49 @Nonnull get(@onnull Context context)50 public static synchronized PermissionsManager get(@Nonnull Context context) { 51 if (sInstance == null) { 52 sInstance = new PermissionsManager(context); 53 } 54 return sInstance; 55 } 56 getNextRequestId()57 private synchronized int getNextRequestId() { 58 return ++mRequestCodeId; 59 } 60 61 requestPermissions(@onnull PermissionsResultCallback callback, @Nullable Activity activity, String... permissionsToRequest)62 public synchronized void requestPermissions(@Nonnull PermissionsResultCallback callback, 63 @Nullable Activity activity, 64 String... permissionsToRequest) { 65 List<String> deniedPermissions = PermissionsUtil.getDeniedPermissions( 66 mContext, permissionsToRequest); 67 if (deniedPermissions.isEmpty()) { 68 return; 69 } 70 // otherwise request the permissions. 71 int requestId = getNextRequestId(); 72 String[] permissionsArray = deniedPermissions.toArray( 73 new String[deniedPermissions.size()]); 74 75 mRequestIdToCallback.put(requestId, callback); 76 if (activity != null) { 77 PermissionsUtil.requestPermissions(activity, requestId, permissionsArray); 78 } else { 79 PermissionsActivity.run(mContext, requestId, permissionsArray); 80 } 81 } 82 onRequestPermissionsResult( int requestCode, String[] permissions, int[] grantResults)83 public synchronized void onRequestPermissionsResult( 84 int requestCode, String[] permissions, int[] grantResults) { 85 PermissionsResultCallback permissionsResultCallback = mRequestIdToCallback.get(requestCode); 86 mRequestIdToCallback.remove(requestCode); 87 88 boolean allGranted = PermissionsUtil.allGranted(grantResults); 89 permissionsResultCallback.onRequestPermissionsResult(allGranted); 90 } 91 } 92