• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.account;
17 
18 import android.accounts.Account;
19 import com.google.android.libraries.mobiledatadownload.internal.MddConstants;
20 import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil;
21 import javax.annotation.Nullable;
22 
23 /** Utils to help with account manipulation. */
24 public final class AccountUtil {
25   private static final String TAG = "AccountUtil";
26   private static final String ACCOUNT_DELIMITER = ":";
27 
AccountUtil()28   private AccountUtil() {}
29 
30   /**
31    * Creates {@link Account} from name and type after validation.
32    *
33    * @return The account instance with the given name and type. Returns null if there is any error.
34    */
35   @Nullable
create(String name, String type)36   public static Account create(String name, String type) {
37     if (!validate(name) || !validate(type)) {
38       LogUtil.e("%s: Unable to create Account with name = '%s', type = '%s'", TAG, name, type);
39       return null;
40     }
41     return new Account(name, type);
42   }
43 
44   /**
45    * Serializes an {@link Account} into a string.
46    *
47    * <p>TODO(b/222110940): make this function consistent with deserialize.
48    */
serialize(Account account)49   public static String serialize(Account account) {
50     return account.type + ACCOUNT_DELIMITER + account.name;
51   }
52 
53   /**
54    * Deserializes a string into an {@link Account}.
55    *
56    * @return The account parsed from string. Returns null if the accountStr is empty or if there is
57    *     any error during parse.
58    */
59   @Nullable
deserialize(String accountStr)60   public static Account deserialize(String accountStr) {
61     if (accountStr.isEmpty()) {
62       return null;
63     }
64     int splitIndex = accountStr.indexOf(ACCOUNT_DELIMITER);
65     if (splitIndex < 0) {
66       LogUtil.e("%s: Unable to parse Account with string = '%s'", TAG, accountStr);
67       return null;
68     }
69     String type = accountStr.substring(0, splitIndex);
70     String name = accountStr.substring(splitIndex + 1);
71     return create(name, type);
72   }
73 
74   /**
75    * Validates whether the field is valid. Returns false if the field is empty or contains delimiter
76    * or contains split char.
77    */
validate(String field)78   private static boolean validate(String field) {
79     return field != null
80         && !field.isEmpty()
81         && !field.contains(ACCOUNT_DELIMITER)
82         && !field.contains(MddConstants.SPLIT_CHAR);
83   }
84 }
85