Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.phone; |
| 18 | |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentValues; |
| 22 | import android.content.Context; |
| 23 | import android.net.Uri; |
| 24 | import android.provider.VoicemailContract; |
| 25 | import android.provider.VoicemailContract.Status; |
| 26 | import android.telecom.PhoneAccountHandle; |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 27 | import com.android.phone.vvm.omtp.VvmLog; |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 28 | |
| 29 | public class VoicemailStatus { |
| 30 | |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 31 | private static final String TAG = "VvmStatus"; |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 32 | |
| 33 | public static class Editor { |
| 34 | |
| 35 | private final Context mContext; |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 36 | @Nullable |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 37 | 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 Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 44 | if (mPhoneAccountHandle == null) { |
| 45 | VvmLog.w(TAG, "VoicemailStatus.Editor created with null phone account, status will" |
| 46 | + " not be written"); |
| 47 | } |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 50 | @Nullable |
Ta-wei Yen | 016df6b | 2016-07-22 14:16:58 -0700 | [diff] [blame] | 51 | public PhoneAccountHandle getPhoneAccountHandle() { |
| 52 | return mPhoneAccountHandle; |
| 53 | } |
| 54 | |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 52b16c6 | 2016-08-19 16:05:34 -0700 | [diff] [blame] | 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() { |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 92 | if (mPhoneAccountHandle == null) { |
Tyler Gunn | 52b16c6 | 2016-08-19 16:05:34 -0700 | [diff] [blame] | 93 | return false; |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 94 | } |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 95 | mValues.put(Status.PHONE_ACCOUNT_COMPONENT_NAME, |
| 96 | mPhoneAccountHandle.getComponentName().flattenToString()); |
| 97 | mValues.put(Status.PHONE_ACCOUNT_ID, mPhoneAccountHandle.getId()); |
| 98 | ContentResolver contentResolver = mContext.getContentResolver(); |
| 99 | Uri statusUri = VoicemailContract.Status.buildSourceUri(mContext.getPackageName()); |
Tyler Gunn | 52b16c6 | 2016-08-19 16:05:34 -0700 | [diff] [blame] | 100 | try { |
| 101 | contentResolver.insert(statusUri, mValues); |
| 102 | } catch (IllegalArgumentException iae) { |
| 103 | VvmLog.e(TAG, "apply :: failed to insert content resolver ", iae); |
| 104 | mValues.clear(); |
| 105 | return false; |
| 106 | } |
Ta-wei Yen | 016df6b | 2016-07-22 14:16:58 -0700 | [diff] [blame] | 107 | mValues.clear(); |
Tyler Gunn | 52b16c6 | 2016-08-19 16:05:34 -0700 | [diff] [blame] | 108 | return true; |
Ta-wei Yen | 016df6b | 2016-07-22 14:16:58 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public ContentValues getValues() { |
| 112 | return mValues; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * A voicemail status editor that the decision of whether to actually write to the database can |
| 118 | * be deferred. This object will be passed around as a usual {@link Editor}, but {@link |
| 119 | * #apply()} doesn't do anything. If later the creator of this object decides any status changes |
| 120 | * written to it should be committed, {@link #deferredApply()} should be called. |
| 121 | */ |
| 122 | public static class DeferredEditor extends Editor { |
| 123 | |
| 124 | private DeferredEditor(Context context, PhoneAccountHandle phoneAccountHandle) { |
| 125 | super(context, phoneAccountHandle); |
| 126 | } |
| 127 | |
| 128 | @Override |
Tyler Gunn | 52b16c6 | 2016-08-19 16:05:34 -0700 | [diff] [blame] | 129 | public boolean apply() { |
Ta-wei Yen | 016df6b | 2016-07-22 14:16:58 -0700 | [diff] [blame] | 130 | // Do nothing |
Tyler Gunn | 52b16c6 | 2016-08-19 16:05:34 -0700 | [diff] [blame] | 131 | return true; |
Ta-wei Yen | 016df6b | 2016-07-22 14:16:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | public void deferredApply() { |
| 135 | super.apply(); |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | public static Editor edit(Context context, PhoneAccountHandle phoneAccountHandle) { |
| 140 | return new Editor(context, phoneAccountHandle); |
| 141 | } |
| 142 | |
Ta-wei Yen | 7eda1e4 | 2016-07-27 11:15:45 -0700 | [diff] [blame] | 143 | /** |
| 144 | * Reset the status to the "disabled" state, which the UI should not show anything for this |
| 145 | * subId. |
| 146 | */ |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 147 | public static void disable(Context context, PhoneAccountHandle phoneAccountHandle) { |
| 148 | edit(context, phoneAccountHandle) |
Ta-wei Yen | 7eda1e4 | 2016-07-27 11:15:45 -0700 | [diff] [blame] | 149 | .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED) |
| 150 | .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION) |
| 151 | .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION) |
| 152 | .apply(); |
| 153 | } |
| 154 | |
Ta-wei Yen | 5d351b9 | 2016-07-28 17:50:32 -0700 | [diff] [blame] | 155 | public static DeferredEditor deferredEdit(Context context, |
| 156 | PhoneAccountHandle phoneAccountHandle) { |
| 157 | return new DeferredEditor(context, phoneAccountHandle); |
Ta-wei Yen | 016df6b | 2016-07-22 14:16:58 -0700 | [diff] [blame] | 158 | } |
Ta-wei Yen | 8a6f86c | 2016-05-25 11:32:32 -0700 | [diff] [blame] | 159 | } |