1 /*
2  * Copyright (C) 2018 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.contacts.common.widget;
18 
19 import android.os.Parcel;
20 import android.os.UserHandle;
21 import android.telecom.PhoneAccount;
22 import android.telecom.PhoneAccountHandle;
23 import com.android.dialer.common.Assert;
24 import com.android.dialer.telecom.TelecomUtil;
25 
26 import com.google.protobuf.ByteString;
27 
28 import java.util.Collection;
29 
30 /** Provides common operation on a {@link SelectPhoneAccountDialogOptions} */
31 public final class SelectPhoneAccountDialogOptionsUtil {
SelectPhoneAccountDialogOptionsUtil()32   private SelectPhoneAccountDialogOptionsUtil() {}
33 
getPhoneAccountHandle( SelectPhoneAccountDialogOptions.Entry entry)34   public static PhoneAccountHandle getPhoneAccountHandle(
35       SelectPhoneAccountDialogOptions.Entry entry) {
36     UserHandle userHandle;
37     Parcel parcel = Parcel.obtain();
38     try {
39       byte[] marshalledUserHandle = entry.getUserHandle().toByteArray();
40       parcel.unmarshall(marshalledUserHandle, 0, marshalledUserHandle.length);
41       parcel.setDataPosition(0);
42       userHandle = parcel.readParcelable(UserHandle.class.getClassLoader());
43     } catch (NullPointerException e) {
44       userHandle = null;
45     }
46     parcel.recycle();
47     return Assert.isNotNull(
48         TelecomUtil.composePhoneAccountHandle(
49             entry.getPhoneAccountHandleComponentName(), entry.getPhoneAccountHandleId(),
50                 userHandle));
51   }
52 
setPhoneAccountHandle( SelectPhoneAccountDialogOptions.Entry.Builder entryBuilder, PhoneAccountHandle phoneAccountHandle)53   public static SelectPhoneAccountDialogOptions.Entry.Builder setPhoneAccountHandle(
54       SelectPhoneAccountDialogOptions.Entry.Builder entryBuilder,
55       PhoneAccountHandle phoneAccountHandle) {
56     entryBuilder.setPhoneAccountHandleComponentName(
57         phoneAccountHandle.getComponentName().flattenToString());
58     entryBuilder.setPhoneAccountHandleId(phoneAccountHandle.getId());
59     Parcel parcel = Parcel.obtain();
60     parcel.writeParcelable(phoneAccountHandle.getUserHandle(), 0);
61     entryBuilder.setUserHandle(ByteString.copyFrom(parcel.marshall()));
62     parcel.recycle();
63     return entryBuilder;
64   }
65 
builderWithAccounts( Collection<PhoneAccountHandle> phoneAccountHandles)66   public static SelectPhoneAccountDialogOptions.Builder builderWithAccounts(
67       Collection<PhoneAccountHandle> phoneAccountHandles) {
68     SelectPhoneAccountDialogOptions.Builder optionsBuilder =
69         SelectPhoneAccountDialogOptions.newBuilder();
70     for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) {
71       optionsBuilder.addEntries(
72           SelectPhoneAccountDialogOptionsUtil.setPhoneAccountHandle(
73               SelectPhoneAccountDialogOptions.Entry.newBuilder(), phoneAccountHandle));
74     }
75     return optionsBuilder;
76   }
77 }
78