1 /* 2 * Copyright (C) 2015 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.messaging.datamodel.data; 18 19 import android.app.LoaderManager; 20 import android.content.Context; 21 import android.content.Loader; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.os.Bundle; 25 26 import com.android.messaging.datamodel.BoundCursorLoader; 27 import com.android.messaging.datamodel.DatabaseHelper.ParticipantColumns; 28 import com.android.messaging.datamodel.MessagingContentProvider; 29 import com.android.messaging.datamodel.binding.BindableData; 30 import com.android.messaging.datamodel.binding.BindingBase; 31 import com.android.messaging.util.Assert; 32 33 /** 34 * Services data needs for BlockedParticipantsFragment 35 */ 36 public class BlockedParticipantsData extends BindableData implements 37 LoaderManager.LoaderCallbacks<Cursor> { 38 public interface BlockedParticipantsDataListener { 39 public void onBlockedParticipantsCursorUpdated(final Cursor cursor); 40 } 41 private static final String BINDING_ID = "bindingId"; 42 private static final int BLOCKED_PARTICIPANTS_LOADER = 1; 43 private final Context mContext; 44 private LoaderManager mLoaderManager; 45 private BlockedParticipantsDataListener mListener; 46 47 public BlockedParticipantsData(final Context context, 48 final BlockedParticipantsDataListener listener) { 49 mContext = context; 50 mListener = listener; 51 } 52 53 @Override 54 public Loader<Cursor> onCreateLoader(final int id, final Bundle args) { 55 Assert.isTrue(id == BLOCKED_PARTICIPANTS_LOADER); 56 final String bindingId = args.getString(BINDING_ID); 57 // Check if data still bound to the requesting ui element 58 if (isBound(bindingId)) { 59 final Uri uri = MessagingContentProvider.PARTICIPANTS_URI; 60 return new BoundCursorLoader(bindingId, mContext, uri, 61 ParticipantData.ParticipantsQuery.PROJECTION, 62 ParticipantColumns.BLOCKED + "=1", null, null); 63 } 64 return null; 65 } 66 67 @Override 68 public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) { 69 Assert.isTrue(loader.getId() == BLOCKED_PARTICIPANTS_LOADER); 70 final BoundCursorLoader cursorLoader = (BoundCursorLoader) loader; 71 Assert.isTrue(isBound(cursorLoader.getBindingId())); 72 mListener.onBlockedParticipantsCursorUpdated(cursor); 73 } 74 75 @Override 76 public void onLoaderReset(final Loader<Cursor> loader) { 77 Assert.isTrue(loader.getId() == BLOCKED_PARTICIPANTS_LOADER); 78 final BoundCursorLoader cursorLoader = (BoundCursorLoader) loader; 79 Assert.isTrue(isBound(cursorLoader.getBindingId())); 80 mListener.onBlockedParticipantsCursorUpdated(null); 81 } 82 83 public void init(final LoaderManager loaderManager, 84 final BindingBase<BlockedParticipantsData> binding) { 85 final Bundle args = new Bundle(); 86 args.putString(BINDING_ID, binding.getBindingId()); 87 mLoaderManager = loaderManager; 88 mLoaderManager.initLoader(BLOCKED_PARTICIPANTS_LOADER, args, this); 89 } 90 91 @Override 92 protected void unregisterListeners() { 93 mListener = null; 94 if (mLoaderManager != null) { 95 mLoaderManager.destroyLoader(BLOCKED_PARTICIPANTS_LOADER); 96 mLoaderManager = null; 97 } 98 } 99 100 public ParticipantListItemData createParticipantListItemData(Cursor cursor) { 101 return new ParticipantListItemData(ParticipantData.getFromCursor(cursor)); 102 } 103 } 104