blob: c5cd52ce64d014065c45c018c396a5a6cd2bf56f [file] [log] [blame]
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -07001/*
2 * Copyright (C) 2016 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
17package com.android.phone;
18
19import android.content.ContentResolver;
20import android.content.ContentValues;
21import android.content.Context;
22import android.net.Uri;
23import android.provider.VoicemailContract;
24import android.provider.VoicemailContract.Status;
25import android.telecom.PhoneAccountHandle;
Ta-wei Yenf5c5d932016-06-01 19:40:24 -070026import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter;
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070027
28public class VoicemailStatus {
29
30
31 public static class Editor {
32
33 private final Context mContext;
34 private final PhoneAccountHandle mPhoneAccountHandle;
35
36 private ContentValues mValues = new ContentValues();
37
38 private Editor(Context context, PhoneAccountHandle phoneAccountHandle) {
39 mContext = context;
40 mPhoneAccountHandle = phoneAccountHandle;
41 }
42
Ta-wei Yen016df6b2016-07-22 14:16:58 -070043 public PhoneAccountHandle getPhoneAccountHandle() {
44 return mPhoneAccountHandle;
45 }
46
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070047 public Editor setType(String type) {
48 mValues.put(Status.SOURCE_TYPE, type);
49 return this;
50 }
51
52 public Editor setConfigurationState(int configurationState) {
53 mValues.put(Status.CONFIGURATION_STATE, configurationState);
54 return this;
55 }
56
57 public Editor setDataChannelState(int dataChannelState) {
58 mValues.put(Status.DATA_CHANNEL_STATE, dataChannelState);
59 return this;
60 }
61
62 public Editor setNotificationChannelState(int notificationChannelState) {
63 mValues.put(Status.NOTIFICATION_CHANNEL_STATE, notificationChannelState);
64 return this;
65 }
66
67 public Editor setQuota(int occupied, int total) {
68 if (occupied == VoicemailContract.Status.QUOTA_UNAVAILABLE
69 && total == VoicemailContract.Status.QUOTA_UNAVAILABLE) {
70 return this;
71 }
72
73 mValues.put(Status.QUOTA_OCCUPIED, occupied);
74 mValues.put(Status.QUOTA_TOTAL, total);
75 return this;
76 }
77
78 public void apply() {
79 mValues.put(Status.PHONE_ACCOUNT_COMPONENT_NAME,
80 mPhoneAccountHandle.getComponentName().flattenToString());
81 mValues.put(Status.PHONE_ACCOUNT_ID, mPhoneAccountHandle.getId());
82 ContentResolver contentResolver = mContext.getContentResolver();
83 Uri statusUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
84 contentResolver.insert(statusUri, mValues);
Ta-wei Yen016df6b2016-07-22 14:16:58 -070085 mValues.clear();
86 }
87
88 public ContentValues getValues() {
89 return mValues;
90 }
91 }
92
93 /**
94 * A voicemail status editor that the decision of whether to actually write to the database can
95 * be deferred. This object will be passed around as a usual {@link Editor}, but {@link
96 * #apply()} doesn't do anything. If later the creator of this object decides any status changes
97 * written to it should be committed, {@link #deferredApply()} should be called.
98 */
99 public static class DeferredEditor extends Editor {
100
101 private DeferredEditor(Context context, PhoneAccountHandle phoneAccountHandle) {
102 super(context, phoneAccountHandle);
103 }
104
105 @Override
106 public void apply() {
107 // Do nothing
108 }
109
110 public void deferredApply() {
111 super.apply();
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -0700112 }
113 }
114
115 public static Editor edit(Context context, PhoneAccountHandle phoneAccountHandle) {
116 return new Editor(context, phoneAccountHandle);
117 }
118
119 public static Editor edit(Context context, int subId) {
Ta-wei Yenf5c5d932016-06-01 19:40:24 -0700120 return new Editor(context, PhoneAccountHandleConverter.fromSubId(subId));
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -0700121 }
Ta-wei Yen016df6b2016-07-22 14:16:58 -0700122
Ta-wei Yen7eda1e42016-07-27 11:15:45 -0700123 /**
124 * Reset the status to the "disabled" state, which the UI should not show anything for this
125 * subId.
126 */
127 public static void disable(Context context, int subId) {
128 edit(context, subId)
129 .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED)
130 .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION)
131 .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION)
132 .apply();
133 }
134
Ta-wei Yen016df6b2016-07-22 14:16:58 -0700135 public static DeferredEditor deferredEdit(Context context, int subId) {
136 return new DeferredEditor(context, PhoneAccountHandleConverter.fromSubId(subId));
137 }
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -0700138}