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                     InputStream inputStream = getContext().getResources().openRawResource(
65                             getContext().getResources().getIdentifier(
66                                     filename.substring(0, filename.lastIndexOf(".")),
67                                     "raw",
68                                     getContext().getPackageName()));
69 
70                     try {
71                         Files.copy(inputStream, imageFile.toPath(),
72                                 StandardCopyOption.REPLACE_EXISTING);
73                     } catch (IOException e) {
74                         Log.e(TAG, "could not copy file to internal cache: " + uri.getPath(), e);
75                     }
76 
77                     imageFile = new File(
78                             getContext().getCacheDir() + File.separator + uri.getLastPathSegment());
79                 }
80 
81                 ParcelFileDescriptor image = ParcelFileDescriptor.open(imageFile,
82                         ParcelFileDescriptor.MODE_READ_ONLY);
83 
84                 return image;
85 
86             default:
87                 return null;
88         }
89     }
90 
91 
92     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)93     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
94             String sortOrder) {
95         return null;
96     }
97 
98     @Override
getType(Uri uri)99     public String getType(Uri uri) {
100         return null;
101     }
102 
103     @Override
insert(Uri uri, ContentValues values)104     public Uri insert(Uri uri, ContentValues values) {
105         return null;
106     }
107 
108     @Override
delete(Uri uri, String selection, String[] selectionArgs)109     public int delete(Uri uri, String selection, String[] selectionArgs) {
110         return 0;
111     }
112 
113     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)114     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
115         return 0;
116     }
117 }
118