1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.drm; 19 20 import android.database.sqlite.SqliteWrapper; 21 22 import android.content.ContentResolver; 23 import android.content.ContentValues; 24 import android.content.Context; 25 import android.net.Uri; 26 import android.util.Log; 27 28 import java.io.IOException; 29 import java.io.OutputStream; 30 31 public class DrmUtils { 32 private static final String TAG = "DrmUtils"; 33 private static final Uri DRM_TEMP_URI = Uri.parse("content://mms/drm"); 34 35 private DrmUtils() { 36 } 37 38 public static void cleanupStorage(Context context) { 39 try { 40 SqliteWrapper.delete(context, context.getContentResolver(), 41 DRM_TEMP_URI, null, null); 42 } catch (RuntimeException e) { 43 // If clearing the temp storage fails, that's not the end of the world; we'll just try 44 // again next time. Could potentially fail in the not-so-understood case described in 45 // b/5415438 (but in any case, we shouldn't kill the app if this fails). 46 Log.e(TAG, e.getMessage(), e); 47 } 48 } 49 50 public static Uri insert(Context context, DrmWrapper drmObj) 51 throws IOException { 52 ContentResolver cr = context.getContentResolver(); 53 Uri uri = SqliteWrapper.insert(context, cr, DRM_TEMP_URI, 54 new ContentValues(0)); 55 OutputStream os = null; 56 try { 57 os = cr.openOutputStream(uri); 58 byte[] data = drmObj.getDecryptedData(); 59 if (data != null) { 60 os.write(data); 61 } 62 return uri; 63 } finally { 64 if (os != null) { 65 try { 66 os.close(); 67 } catch (IOException e) { 68 Log.e(TAG, e.getMessage(), e); 69 } 70 } 71 } 72 } 73 } 74