summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/datamodel/data/MediaPickerData.java
blob: b0c8bf7345a96cfee9a94b5769650295a6439e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.datamodel.data;

import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.messaging.datamodel.BoundCursorLoader;
import com.android.messaging.datamodel.GalleryBoundCursorLoader;
import com.android.messaging.datamodel.binding.BindableData;
import com.android.messaging.datamodel.binding.BindingBase;
import com.android.messaging.util.Assert;
import com.android.messaging.util.BuglePrefs;
import com.android.messaging.util.BuglePrefsKeys;
import com.android.messaging.util.LogUtil;

/**
 * Services data needs for MediaPicker.
 */
public class MediaPickerData extends BindableData {
    public interface MediaPickerDataListener {
        void onMediaPickerDataUpdated(MediaPickerData mediaPickerData, Object data, int loaderId);
    }

    private static final String BINDING_ID = "bindingId";
    private final Context mContext;
    private LoaderManager mLoaderManager;
    private final GalleryLoaderCallbacks mGalleryLoaderCallbacks;
    private MediaPickerDataListener mListener;

    public MediaPickerData(final Context context) {
        mContext = context;
        mGalleryLoaderCallbacks = new GalleryLoaderCallbacks();
    }

    public static final int GALLERY_IMAGE_LOADER = 1;

    /**
     * A trampoline class so that we can inherit from LoaderManager.LoaderCallbacks multiple times.
     */
    private class GalleryLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> {
        @Override
        public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
            final String bindingId = args.getString(BINDING_ID);
            // Check if data still bound to the requesting ui element
            if (isBound(bindingId)) {
                switch (id) {
                    case GALLERY_IMAGE_LOADER:
                        return new GalleryBoundCursorLoader(bindingId, mContext);

                    default:
                        Assert.fail("Unknown loader id for gallery picker!");
                        break;
                }
            } else {
                LogUtil.w(LogUtil.BUGLE_TAG, "Loader created after unbinding the media picker");
            }
            return null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) {
            final BoundCursorLoader cursorLoader = (BoundCursorLoader) loader;
            if (isBound(cursorLoader.getBindingId())) {
                switch (loader.getId()) {
                    case GALLERY_IMAGE_LOADER:
                        mListener.onMediaPickerDataUpdated(MediaPickerData.this, data,
                                GALLERY_IMAGE_LOADER);
                        break;

                    default:
                        Assert.fail("Unknown loader id for gallery picker!");
                        break;
                }
            } else {
                LogUtil.w(LogUtil.BUGLE_TAG, "Loader finished after unbinding the media picker");
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onLoaderReset(final Loader<Cursor> loader) {
            final BoundCursorLoader cursorLoader = (BoundCursorLoader) loader;
            if (isBound(cursorLoader.getBindingId())) {
                switch (loader.getId()) {
                    case GALLERY_IMAGE_LOADER:
                        mListener.onMediaPickerDataUpdated(MediaPickerData.this, null,
                                GALLERY_IMAGE_LOADER);
                        break;

                    default:
                        Assert.fail("Unknown loader id for media picker!");
                        break;
                }
            } else {
                LogUtil.w(LogUtil.BUGLE_TAG, "Loader reset after unbinding the media picker");
            }
        }
    }



    public void startLoader(final int loaderId, final BindingBase<MediaPickerData> binding,
            @Nullable Bundle args, final MediaPickerDataListener listener) {
        if (args == null) {
            args = new Bundle();
        }
        args.putString(BINDING_ID, binding.getBindingId());
        if (loaderId == GALLERY_IMAGE_LOADER) {
            mLoaderManager.initLoader(loaderId, args, mGalleryLoaderCallbacks).forceLoad();
        } else {
            Assert.fail("Unsupported loader id for media picker!");
        }
        mListener = listener;
    }

    public void destroyLoader(final int loaderId) {
        mLoaderManager.destroyLoader(loaderId);
    }

    public void init(final LoaderManager loaderManager) {
        mLoaderManager = loaderManager;
    }

    @Override
    protected void unregisterListeners() {
        // This could be null if we bind but the caller doesn't init the BindableData
        if (mLoaderManager != null) {
            mLoaderManager.destroyLoader(GALLERY_IMAGE_LOADER);
            mLoaderManager = null;
        }
    }

    /**
     * Gets the last selected chooser index, or -1 if no selection has been saved.
     */
    public int getSelectedChooserIndex() {
        return BuglePrefs.getApplicationPrefs().getInt(
                BuglePrefsKeys.SELECTED_MEDIA_PICKER_CHOOSER_INDEX,
                BuglePrefsKeys.SELECTED_MEDIA_PICKER_CHOOSER_INDEX_DEFAULT);
    }

    /**
     * Saves the selected media chooser index.
     * @param selectedIndex the selected media chooser index.
     */
    public void saveSelectedChooserIndex(final int selectedIndex) {
        BuglePrefs.getApplicationPrefs().putInt(BuglePrefsKeys.SELECTED_MEDIA_PICKER_CHOOSER_INDEX,
                selectedIndex);
    }

}