1 /* 2 * Copyright (C) 2010 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.contacts.list; 17 18 import android.content.ContentResolver; 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.database.Cursor; 23 import android.os.Handler; 24 import android.provider.ContactsContract.ProviderStatus; 25 26 /** 27 * Checks provider status and configures a list adapter accordingly. 28 */ 29 public class ProviderStatusLoader extends ContentObserver { 30 31 /** 32 * Callback interface invoked when the provider status changes. 33 */ 34 public interface ProviderStatusListener { 35 public void onProviderStatusChange(); 36 } 37 38 private static final String[] PROJECTION = new String[] { 39 ProviderStatus.STATUS, 40 ProviderStatus.DATA1 41 }; 42 43 private static final int UNKNOWN = -1; 44 45 private final Context mContext; 46 private int mProviderStatus = UNKNOWN; 47 private String mProviderData; 48 private ProviderStatusListener mListener; 49 private Handler mHandler = new Handler(); 50 51 public ProviderStatusLoader(Context context) { 52 super(null); 53 this.mContext = context; 54 } 55 56 public int getProviderStatus() { 57 if (mProviderStatus == UNKNOWN) { 58 loadProviderStatus(); 59 } 60 61 return mProviderStatus; 62 } 63 64 public String getProviderStatusData() { 65 if (mProviderStatus == UNKNOWN) { 66 loadProviderStatus(); 67 } 68 69 return mProviderData; 70 } 71 72 protected void loadProviderStatus() { 73 74 // Default to normal status 75 mProviderStatus = ProviderStatus.STATUS_NORMAL; 76 77 // This query can be performed on the UI thread because 78 // the API explicitly allows such use. 79 Cursor cursor = mContext.getContentResolver().query(ProviderStatus.CONTENT_URI, 80 PROJECTION, null, null, null); 81 if (cursor != null) { 82 try { 83 if (cursor.moveToFirst()) { 84 mProviderStatus = cursor.getInt(0); 85 mProviderData = cursor.getString(1); 86 } 87 } finally { 88 cursor.close(); 89 } 90 } 91 } 92 93 public void setProviderStatusListener(ProviderStatusListener listener) { 94 mListener = listener; 95 96 ContentResolver resolver = mContext.getContentResolver(); 97 if (listener != null) { 98 mProviderStatus = UNKNOWN; 99 resolver.registerContentObserver(ProviderStatus.CONTENT_URI, false, this); 100 } else { 101 resolver.unregisterContentObserver(this); 102 } 103 } 104 105 @Override 106 public void onChange(boolean selfChange) { 107 // Deliver a notification on the UI thread 108 mHandler.post(new Runnable() { 109 @Override 110 public void run() { 111 if (mListener != null) { 112 mProviderStatus = UNKNOWN; 113 mListener.onProviderStatusChange(); 114 } 115 } 116 }); 117 } 118 119 /** 120 * Sends a provider status update, which will trigger a retry of database upgrade 121 */ 122 public void retryUpgrade() { 123 ContentValues values = new ContentValues(); 124 values.put(ProviderStatus.STATUS, ProviderStatus.STATUS_UPGRADING); 125 mContext.getContentResolver().update(ProviderStatus.CONTENT_URI, values, null, null); 126 } 127 } 128