1/*
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.voicemail.impl;
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 private final PhoneAccountHandle mPhoneAccountHandle;
36
37    private ContentValues mValues = new ContentValues();
38
39    private Editor(Context context, PhoneAccountHandle phoneAccountHandle) {
40      mContext = context;
41      mPhoneAccountHandle = phoneAccountHandle;
42      if (mPhoneAccountHandle == null) {
43        VvmLog.w(
44            TAG,
45            "VoicemailStatus.Editor created with null phone account, status will"
46                + " not be written");
47      }
48    }
49
50    @Nullable
51    public PhoneAccountHandle getPhoneAccountHandle() {
52      return mPhoneAccountHandle;
53    }
54
55    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    /**
87     * Apply the changes to the {@link VoicemailStatus} {@link #Editor}.
88     *
89     * @return {@code true} if the changes were successfully applied, {@code false} otherwise.
90     */
91    public boolean apply() {
92      if (mPhoneAccountHandle == null) {
93        return false;
94      }
95      mValues.put(
96          Status.PHONE_ACCOUNT_COMPONENT_NAME,
97          mPhoneAccountHandle.getComponentName().flattenToString());
98      mValues.put(Status.PHONE_ACCOUNT_ID, mPhoneAccountHandle.getId());
99      ContentResolver contentResolver = mContext.getContentResolver();
100      Uri statusUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName());
101      try {
102        contentResolver.insert(statusUri, mValues);
103      } catch (IllegalArgumentException iae) {
104        VvmLog.e(TAG, "apply :: failed to insert content resolver ", iae);
105        mValues.clear();
106        return false;
107      }
108      mValues.clear();
109      return true;
110    }
111
112    public ContentValues getValues() {
113      return mValues;
114    }
115  }
116
117  /**
118   * A voicemail status editor that the decision of whether to actually write to the database can be
119   * deferred. This object will be passed around as a usual {@link Editor}, but {@link #apply()}
120   * doesn't do anything. If later the creator of this object decides any status changes written to
121   * it should be committed, {@link #deferredApply()} should be called.
122   */
123  public static class DeferredEditor extends Editor {
124
125    private DeferredEditor(Context context, PhoneAccountHandle phoneAccountHandle) {
126      super(context, phoneAccountHandle);
127    }
128
129    @Override
130    public boolean apply() {
131      // Do nothing
132      return true;
133    }
134
135    public void deferredApply() {
136      super.apply();
137    }
138  }
139
140  public static Editor edit(Context context, PhoneAccountHandle phoneAccountHandle) {
141    return new Editor(context, phoneAccountHandle);
142  }
143
144  /**
145   * Reset the status to the "disabled" state, which the UI should not show anything for this
146   * phoneAccountHandle.
147   */
148  public static void disable(Context context, PhoneAccountHandle phoneAccountHandle) {
149    edit(context, phoneAccountHandle)
150        .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED)
151        .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION)
152        .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION)
153        .apply();
154  }
155
156  public static DeferredEditor deferredEdit(
157      Context context, PhoneAccountHandle phoneAccountHandle) {
158    return new DeferredEditor(context, phoneAccountHandle);
159  }
160}
161