blob: 4d7d388bb06ae5beec98c3ea323323d0cfd15cf1 [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
Ta-wei Yen5d351b92016-07-28 17:50:32 -070019import android.annotation.Nullable;
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070020import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.content.Context;
23import android.net.Uri;
24import android.provider.VoicemailContract;
25import android.provider.VoicemailContract.Status;
26import android.telecom.PhoneAccountHandle;
Ta-wei Yen5d351b92016-07-28 17:50:32 -070027import com.android.phone.vvm.omtp.VvmLog;
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070028
29public class VoicemailStatus {
30
Ta-wei Yen5d351b92016-07-28 17:50:32 -070031 private static final String TAG = "VvmStatus";
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070032
33 public static class Editor {
34
35 private final Context mContext;
Ta-wei Yen5d351b92016-07-28 17:50:32 -070036 @Nullable
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070037 private final PhoneAccountHandle mPhoneAccountHandle;
38
39 private ContentValues mValues = new ContentValues();
40
41 private Editor(Context context, PhoneAccountHandle phoneAccountHandle) {
42 mContext = context;
43 mPhoneAccountHandle = phoneAccountHandle;
Ta-wei Yen5d351b92016-07-28 17:50:32 -070044 if (mPhoneAccountHandle == null) {
45 VvmLog.w(TAG, "VoicemailStatus.Editor created with null phone account, status will"
46 + " not be written");
47 }
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070048 }
49
Ta-wei Yen5d351b92016-07-28 17:50:32 -070050 @Nullable
Ta-wei Yen016df6b2016-07-22 14:16:58 -070051 public PhoneAccountHandle getPhoneAccountHandle() {
52 return mPhoneAccountHandle;
53 }
54
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070055 public Editor setType(String type) {
56 mValues.put(Status.SOURCE_TYPE, type);
57 return this;
58 }
59
60 public Editor setConfigurationState(int configurationState) {
61 mValues.put(Status.CONFIGURATION_STATE, configurationState);
62 return this;
63 }
64
65 public Editor setDataChannelState(int dataChannelState) {
66 mValues.put(Status.DATA_CHANNEL_STATE, dataChannelState);
67 return this;
68 }
69
70 public Editor setNotificationChannelState(int notificationChannelState) {
71 mValues.put(Status.NOTIFICATION_CHANNEL_STATE, notificationChannelState);
72 return this;
73 }
74
75 public Editor setQuota(int occupied, int total) {
76 if (occupied == VoicemailContract.Status.QUOTA_UNAVAILABLE
77 && total == VoicemailContract.Status.QUOTA_UNAVAILABLE) {
78 return this;
79 }
80
81 mValues.put(Status.QUOTA_OCCUPIED, occupied);
82 mValues.put(Status.QUOTA_TOTAL, total);
83 return this;
84 }
85
86 public void apply() {
Ta-wei Yen5d351b92016-07-28 17:50:32 -070087 if (mPhoneAccountHandle == null) {
88 return;
89 }
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -070090 mValues.put(Status.PHONE_ACCOUNT_COMPONENT_NAME,
91 mPhoneAccountHandle.getComponentName().flattenToString());
92 mValues.put(Status.PHONE_ACCOUNT_ID, mPhoneAccountHandle.getId());
93 ContentResolver contentResolver = mContext.getContentResolver();
94 Uri statusUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
95 contentResolver.insert(statusUri, mValues);
Ta-wei Yen016df6b2016-07-22 14:16:58 -070096 mValues.clear();
97 }
98
99 public ContentValues getValues() {
100 return mValues;
101 }
102 }
103
104 /**
105 * A voicemail status editor that the decision of whether to actually write to the database can
106 * be deferred. This object will be passed around as a usual {@link Editor}, but {@link
107 * #apply()} doesn't do anything. If later the creator of this object decides any status changes
108 * written to it should be committed, {@link #deferredApply()} should be called.
109 */
110 public static class DeferredEditor extends Editor {
111
112 private DeferredEditor(Context context, PhoneAccountHandle phoneAccountHandle) {
113 super(context, phoneAccountHandle);
114 }
115
116 @Override
117 public void apply() {
118 // Do nothing
119 }
120
121 public void deferredApply() {
122 super.apply();
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -0700123 }
124 }
125
126 public static Editor edit(Context context, PhoneAccountHandle phoneAccountHandle) {
127 return new Editor(context, phoneAccountHandle);
128 }
129
Ta-wei Yen7eda1e42016-07-27 11:15:45 -0700130 /**
131 * Reset the status to the "disabled" state, which the UI should not show anything for this
132 * subId.
133 */
Ta-wei Yen5d351b92016-07-28 17:50:32 -0700134 public static void disable(Context context, PhoneAccountHandle phoneAccountHandle) {
135 edit(context, phoneAccountHandle)
Ta-wei Yen7eda1e42016-07-27 11:15:45 -0700136 .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED)
137 .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION)
138 .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION)
139 .apply();
140 }
141
Ta-wei Yen5d351b92016-07-28 17:50:32 -0700142 public static DeferredEditor deferredEdit(Context context,
143 PhoneAccountHandle phoneAccountHandle) {
144 return new DeferredEditor(context, phoneAccountHandle);
Ta-wei Yen016df6b2016-07-22 14:16:58 -0700145 }
Ta-wei Yen8a6f86c2016-05-25 11:32:32 -0700146}