Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2011 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.providers.media;
     18 
     19 import java.io.File;
     20 
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.SharedPreferences;
     25 import android.database.sqlite.SQLiteDatabase;
     26 import android.os.RemoteException;
     27 import android.util.Log;
     28 import android.util.Slog;
     29 
     30 /**
     31  * This will be launched during system boot, after the core system has
     32  * been brought up but before any non-persistent processes have been
     33  * started.  It is launched in a special state, with no content provider
     34  * or custom application class associated with the process running.
     35  *
     36  * It's job is to prime the contacts database. Either create it
     37  * if it doesn't exist, or open it and force any necessary upgrades.
     38  * All of this heavy lifting happens before the boot animation ends.
     39  */
     40 public class MediaUpgradeReceiver extends BroadcastReceiver {
     41     static final String TAG = "MediaUpgradeReceiver";
     42     static final String PREF_DB_VERSION = "db_version";
     43 
     44     @Override
     45     public void onReceive(Context context, Intent intent) {
     46         // We are now running with the system up, but no apps started,
     47         // so can do whatever cleanup after an upgrade that we want.
     48 
     49         // Lookup the last known database version
     50         SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
     51         int prefVersion = prefs.getInt(PREF_DB_VERSION, 0);
     52         int dbVersion = MediaProvider.getDatabaseVersion(context);
     53         if (prefVersion == dbVersion) {
     54             return;
     55         }
     56         prefs.edit().putInt(PREF_DB_VERSION, dbVersion).commit();
     57 
     58         try {
     59             File dbDir = context.getDatabasePath("foo").getParentFile();
     60             String[] files = dbDir.list();
     61             if (files == null) return;
     62             for (int i=0; i<files.length; i++) {
     63                 String file = files[i];
     64                 if (MediaProvider.isMediaDatabaseName(file)) {
     65                     long startTime = System.currentTimeMillis();
     66                     Slog.i(TAG, "---> Start upgrade of media database " + file);
     67                     SQLiteDatabase db = null;
     68                     try {
     69                         MediaProvider.DatabaseHelper helper = new MediaProvider.DatabaseHelper(
     70                                 context, file, MediaProvider.isInternalMediaDatabaseName(file),
     71                                 false, null);
     72                         db = helper.getWritableDatabase();
     73                     } catch (Throwable t) {
     74                         Log.wtf(TAG, "Error during upgrade of media db " + file, t);
     75                     } finally {
     76                         if (db != null) {
     77                             db.close();
     78                         }
     79                     }
     80                     Slog.i(TAG, "<--- Finished upgrade of media database " + file
     81                             + " in " + (System.currentTimeMillis()-startTime) + "ms");
     82                 }
     83             }
     84         } catch (Throwable t) {
     85             // Something has gone terribly wrong.
     86             Log.wtf(TAG, "Error during upgrade attempt.", t);
     87         }
     88     }
     89 }
     90