1 /*
2  * Copyright (C) 2018 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.google.android.car.kitchensink.cluster;
17 
18 import android.content.ContentProvider;
19 import android.content.ContentValues;
20 import android.content.UriMatcher;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25 
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.StandardCopyOption;
32 
33 /**
34  * Image Content Provider for the car instument cluster
35  */
36 public class ClusterContentProvider extends ContentProvider {
37     private static final String TAG = "ClusterContentProvider";
38     private static final String AUTHORITY =
39             "com.google.android.car.kitchensink.cluster.clustercontentprovider";
40 
41     private UriMatcher mUriMatcher;
42     private static final int URI_IMAGE_CODE = 1;
43 
44     @Override
onCreate()45     public boolean onCreate() {
46         mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
47         mUriMatcher.addURI(AUTHORITY, "img/*", URI_IMAGE_CODE);
48 
49         return true;
50     }
51 
52     @Override
openFile(Uri uri, String mode)53     public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
54         switch (mUriMatcher.match(uri)) {
55             case URI_IMAGE_CODE:
56                 // Tries to get the img file from internal cache
57                 String filename = new File(uri.getPath()).getName();
58                 File imageFile = new File(
59                         getContext().getCacheDir() + File.separator + uri.getLastPathSegment());
60 
61                 // If the file doesn't exist in internal cache,
62                 // copy the file from res.raw into internal cache
63                 if (!imageFile.exists()) {
64                     try (InputStream inputStream = getContext().getResources().openRawResource(
65                             getContext().getResources().getIdentifier(
66                                     filename.substring(0, filename.lastIndexOf(".")),
67                                     "raw",
68                                     getContext().getPackageName()))) {
69                         Files.copy(inputStream, imageFile.toPath(),
70                                 StandardCopyOption.REPLACE_EXISTING);
71                     } catch (IOException e) {
72                         Log.e(TAG, "could not copy file to internal cache: " + uri.getPath(), e);
73                     }
74 
75                     imageFile = new File(
76                             getContext().getCacheDir() + File.separator + uri.getLastPathSegment());
77                 }
78 
79                 ParcelFileDescriptor image = ParcelFileDescriptor.open(imageFile,
80                         ParcelFileDescriptor.MODE_READ_ONLY);
81 
82                 return image;
83 
84             default:
85                 return null;
86         }
87     }
88 
89 
90     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)91     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
92             String sortOrder) {
93         return null;
94     }
95 
96     @Override
getType(Uri uri)97     public String getType(Uri uri) {
98         return null;
99     }
100 
101     @Override
insert(Uri uri, ContentValues values)102     public Uri insert(Uri uri, ContentValues values) {
103         return null;
104     }
105 
106     @Override
delete(Uri uri, String selection, String[] selectionArgs)107     public int delete(Uri uri, String selection, String[] selectionArgs) {
108         return 0;
109     }
110 
111     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)112     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
113         return 0;
114     }
115 }
116