blob: 63007932eb145cde0e3de9585899fa569b11e060 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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.voicemailomtp;
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.support.annotation.Nullable;
26import android.telecom.PhoneAccountHandle;
27
28public class VoicemailStatus {
29
30 private static final String TAG = "VvmStatus";
31
32 public static class Editor {
33
34 private final Context mContext;
35 @Nullable
36 private final PhoneAccountHandle mPhoneAccountHandle;
37
38 private ContentValues mValues = new ContentValues();
39
40 private Editor(Context context, PhoneAccountHandle phoneAccountHandle) {
41 mContext = context;
42 mPhoneAccountHandle = phoneAccountHandle;
43 if (mPhoneAccountHandle == null) {
44 VvmLog.w(TAG, "VoicemailStatus.Editor created with null phone account, status will"
45 + " not be written");
46 }
47 }
48
49 @Nullable
50 public PhoneAccountHandle getPhoneAccountHandle() {
51 return mPhoneAccountHandle;
52 }
53
54 public Editor setType(String type) {
55 mValues.put(Status.SOURCE_TYPE, type);
56 return this;
57 }
58
59 public Editor setConfigurationState(int configurationState) {
60 mValues.put(Status.CONFIGURATION_STATE, configurationState);
61 return this;
62 }
63
64 public Editor setDataChannelState(int dataChannelState) {
65 mValues.put(Status.DATA_CHANNEL_STATE, dataChannelState);
66 return this;
67 }
68
69 public Editor setNotificationChannelState(int notificationChannelState) {
70 mValues.put(Status.NOTIFICATION_CHANNEL_STATE, notificationChannelState);
71 return this;
72 }
73
74 public Editor setQuota(int occupied, int total) {
75 if (occupied == VoicemailContract.Status.QUOTA_UNAVAILABLE
76 && total == VoicemailContract.Status.QUOTA_UNAVAILABLE) {
77 return this;
78 }
79
80 mValues.put(Status.QUOTA_OCCUPIED, occupied);
81 mValues.put(Status.QUOTA_TOTAL, total);
82 return this;
83 }
84
85 /**
86 * Apply the changes to the {@link VoicemailStatus} {@link #Editor}.
87 *
88 * @return {@code true} if the changes were successfully applied, {@code false} otherwise.
89 */
90 public boolean apply() {
91 if (mPhoneAccountHandle == null) {
92 return false;
93 }
94 mValues.put(Status.PHONE_ACCOUNT_COMPONENT_NAME,
95 mPhoneAccountHandle.getComponentName().flattenToString());
96 mValues.put(Status.PHONE_ACCOUNT_ID, mPhoneAccountHandle.getId());
97 ContentResolver contentResolver = mContext.getContentResolver();
98 Uri statusUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
99 try {
100 contentResolver.insert(statusUri, mValues);
101 } catch (IllegalArgumentException iae) {
102 VvmLog.e(TAG, "apply :: failed to insert content resolver ", iae);
103 mValues.clear();
104 return false;
105 }
106 mValues.clear();
107 return true;
108 }
109
110 public ContentValues getValues() {
111 return mValues;
112 }
113 }
114
115 /**
116 * A voicemail status editor that the decision of whether to actually write to the database can
117 * be deferred. This object will be passed around as a usual {@link Editor}, but {@link
118 * #apply()} doesn't do anything. If later the creator of this object decides any status changes
119 * written to it should be committed, {@link #deferredApply()} should be called.
120 */
121 public static class DeferredEditor extends Editor {
122
123 private DeferredEditor(Context context, PhoneAccountHandle phoneAccountHandle) {
124 super(context, phoneAccountHandle);
125 }
126
127 @Override
128 public boolean apply() {
129 // Do nothing
130 return true;
131 }
132
133 public void deferredApply() {
134 super.apply();
135 }
136 }
137
138 public static Editor edit(Context context, PhoneAccountHandle phoneAccountHandle) {
139 return new Editor(context, phoneAccountHandle);
140 }
141
142 /**
143 * Reset the status to the "disabled" state, which the UI should not show anything for this
144 * phoneAccountHandle.
145 */
146 public static void disable(Context context, PhoneAccountHandle phoneAccountHandle) {
147 edit(context, phoneAccountHandle)
148 .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED)
149 .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION)
150 .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION)
151 .apply();
152 }
153
154 public static DeferredEditor deferredEdit(Context context,
155 PhoneAccountHandle phoneAccountHandle) {
156 return new DeferredEditor(context, phoneAccountHandle);
157 }
158}