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;
18 
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.net.Uri;
22 import android.text.TextUtils;
23 
24 import com.android.messaging.Factory;
25 import com.android.messaging.datamodel.action.Action;
26 import com.android.messaging.datamodel.action.ActionService;
27 import com.android.messaging.datamodel.action.BackgroundWorker;
28 import com.android.messaging.datamodel.data.BlockedParticipantsData;
29 import com.android.messaging.datamodel.data.BlockedParticipantsData.BlockedParticipantsDataListener;
30 import com.android.messaging.datamodel.data.ContactListItemData;
31 import com.android.messaging.datamodel.data.ContactPickerData;
32 import com.android.messaging.datamodel.data.ContactPickerData.ContactPickerDataListener;
33 import com.android.messaging.datamodel.data.ConversationData;
34 import com.android.messaging.datamodel.data.ConversationData.ConversationDataListener;
35 import com.android.messaging.datamodel.data.ConversationListData;
36 import com.android.messaging.datamodel.data.ConversationListData.ConversationListDataListener;
37 import com.android.messaging.datamodel.data.DraftMessageData;
38 import com.android.messaging.datamodel.data.GalleryGridItemData;
39 import com.android.messaging.datamodel.data.LaunchConversationData;
40 import com.android.messaging.datamodel.data.LaunchConversationData.LaunchConversationDataListener;
41 import com.android.messaging.datamodel.data.MediaPickerData;
42 import com.android.messaging.datamodel.data.MessagePartData;
43 import com.android.messaging.datamodel.data.ParticipantData;
44 import com.android.messaging.datamodel.data.ParticipantListItemData;
45 import com.android.messaging.datamodel.data.PeopleAndOptionsData;
46 import com.android.messaging.datamodel.data.PeopleAndOptionsData.PeopleAndOptionsDataListener;
47 import com.android.messaging.datamodel.data.PeopleOptionsItemData;
48 import com.android.messaging.datamodel.data.SettingsData;
49 import com.android.messaging.datamodel.data.SettingsData.SettingsDataListener;
50 import com.android.messaging.datamodel.data.SubscriptionListData;
51 import com.android.messaging.datamodel.data.VCardContactItemData;
52 import com.android.messaging.util.Assert.DoesNotRunOnMainThread;
53 
54 public abstract class DataModel {
55     private String mFocusedConversation;
56     private boolean mConversationListScrolledToNewestConversation;
57 
get()58     public static DataModel get() {
59         return Factory.get().getDataModel();
60     }
61 
startActionService(final Action action)62     public static final void startActionService(final Action action) {
63         get().getActionService().startAction(action);
64     }
65 
scheduleAction(final Action action, final int code, final long delayMs)66     public static final void scheduleAction(final Action action,
67             final int code, final long delayMs) {
68         get().getActionService().scheduleAction(action, code, delayMs);
69     }
70 
createConversationListData(final Context context, final ConversationListDataListener listener, final boolean archivedMode)71     public abstract ConversationListData createConversationListData(final Context context,
72             final ConversationListDataListener listener, final boolean archivedMode);
73 
createConversationData(final Context context, final ConversationDataListener listener, final String conversationId)74     public abstract ConversationData createConversationData(final Context context,
75             final ConversationDataListener listener, final String conversationId);
76 
createContactListItemData()77     public abstract ContactListItemData createContactListItemData();
78 
createContactPickerData(final Context context, final ContactPickerDataListener listener)79     public abstract ContactPickerData createContactPickerData(final Context context,
80             final ContactPickerDataListener listener);
81 
createMediaPickerData(final Context context)82     public abstract MediaPickerData createMediaPickerData(final Context context);
83 
createGalleryGridItemData()84     public abstract GalleryGridItemData createGalleryGridItemData();
85 
createLaunchConversationData( LaunchConversationDataListener listener)86     public abstract LaunchConversationData createLaunchConversationData(
87             LaunchConversationDataListener listener);
88 
createPeopleOptionsItemData(final Context context)89     public abstract PeopleOptionsItemData createPeopleOptionsItemData(final Context context);
90 
createPeopleAndOptionsData(final String conversationId, final Context context, final PeopleAndOptionsDataListener listener)91     public abstract PeopleAndOptionsData createPeopleAndOptionsData(final String conversationId,
92             final Context context, final PeopleAndOptionsDataListener listener);
93 
createVCardContactItemData(final Context context, final MessagePartData data)94     public abstract VCardContactItemData createVCardContactItemData(final Context context,
95             final MessagePartData data);
96 
createVCardContactItemData(final Context context, final Uri vCardUri)97     public abstract VCardContactItemData createVCardContactItemData(final Context context,
98             final Uri vCardUri);
99 
createParticipantListItemData( final ParticipantData participant)100     public abstract ParticipantListItemData createParticipantListItemData(
101             final ParticipantData participant);
102 
createBlockedParticipantsData(Context context, BlockedParticipantsDataListener listener)103     public abstract BlockedParticipantsData createBlockedParticipantsData(Context context,
104             BlockedParticipantsDataListener listener);
105 
createSubscriptonListData(Context context)106     public abstract SubscriptionListData createSubscriptonListData(Context context);
107 
createSettingsData(Context context, SettingsDataListener listener)108     public abstract SettingsData createSettingsData(Context context, SettingsDataListener listener);
109 
createDraftMessageData(String conversationId)110     public abstract DraftMessageData createDraftMessageData(String conversationId);
111 
getActionService()112     public abstract ActionService getActionService();
113 
getBackgroundWorkerForActionService()114     public abstract BackgroundWorker getBackgroundWorkerForActionService();
115 
116     @DoesNotRunOnMainThread
getDatabase()117     public abstract DatabaseWrapper getDatabase();
118 
119     // Allow DataModel to coordinate with activity lifetime events.
onActivityResume()120     public abstract void onActivityResume();
121 
onCreateTables(final SQLiteDatabase db)122     abstract void onCreateTables(final SQLiteDatabase db);
123 
setFocusedConversation(final String conversationId)124     public void setFocusedConversation(final String conversationId) {
125         mFocusedConversation = conversationId;
126     }
127 
isFocusedConversation(final String conversationId)128     public boolean isFocusedConversation(final String conversationId) {
129         return !TextUtils.isEmpty(mFocusedConversation)
130                 && TextUtils.equals(mFocusedConversation, conversationId);
131     }
132 
setConversationListScrolledToNewestConversation( final boolean scrolledToNewestConversation)133     public void setConversationListScrolledToNewestConversation(
134             final boolean scrolledToNewestConversation) {
135         mConversationListScrolledToNewestConversation = scrolledToNewestConversation;
136     }
137 
isConversationListScrolledToNewestConversation()138     public boolean isConversationListScrolledToNewestConversation() {
139         return mConversationListScrolledToNewestConversation;
140     }
141 
142     /**
143      * If a new message is received in the specified conversation, will the user be able to
144      * observe it in some UI within the app?
145      * @param conversationId conversation with the new incoming message
146      */
isNewMessageObservable(final String conversationId)147     public boolean isNewMessageObservable(final String conversationId) {
148         return isConversationListScrolledToNewestConversation()
149                 || isFocusedConversation(conversationId);
150     }
151 
onApplicationCreated()152     public abstract void onApplicationCreated();
153 
getSyncManager()154     public abstract SyncManager getSyncManager();
155 }
156