• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 package com.android.car.media.localmediaplayer;
17 
18 import android.content.ContentResolver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.res.Resources;
23 import android.net.Uri;
24 
25 public class Utils {
26     static final String[] PERMISSIONS = {
27         android.Manifest.permission.READ_EXTERNAL_STORAGE
28     };
29 
getUriForResource(Context context, int id)30     static Uri getUriForResource(Context context, int id) {
31         Resources res = context.getResources();
32         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
33                 + "://" + res.getResourcePackageName(id)
34                 + "/" + res.getResourceTypeName(id)
35                 + "/" + res.getResourceEntryName(id));
36     }
37 
hasRequiredPermissions(Context context)38     static boolean hasRequiredPermissions(Context context) {
39         for (String permission : PERMISSIONS) {
40             if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
41                 return false;
42             }
43         }
44         return true;
45     }
46 
startPermissionRequest(Context context)47     static void startPermissionRequest(Context context) {
48         if (!Utils.hasRequiredPermissions(context)) {
49             Intent intent = new Intent(context, PermissionsActivity.class);
50             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
51             context.startActivity(intent);
52         }
53     }
54 }
55