blob: e6acc64508a7a79834849ffb9fc0fde17be67582 [file] [log] [blame]
Chiao Chengbeca8562012-11-28 18:06:44 -08001/*
2 * Copyright (C) 2010 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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.util;
Chiao Chengbeca8562012-11-28 18:06:44 -080018
Ihab Awad413589f2014-07-02 14:00:28 -070019import android.accounts.AccountManager;
Chiao Chengbeca8562012-11-28 18:06:44 -080020import android.accounts.AuthenticatorDescription;
21import android.content.Context;
Chiao Chengbeca8562012-11-28 18:06:44 -080022import android.content.pm.PackageManager;
23import android.content.pm.PackageManager.NameNotFoundException;
Chiao Chengbeca8562012-11-28 18:06:44 -080024import android.content.res.Resources;
25import android.content.res.Resources.NotFoundException;
26import android.content.res.TypedArray;
27import android.content.res.XmlResourceParser;
28import android.util.AttributeSet;
Chiao Chengbeca8562012-11-28 18:06:44 -080029import android.util.Xml;
30
Arthur Wang3f6a2442016-12-05 14:51:59 -080031import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080032import com.android.contacts.model.account.ExternalAccountType;
Yorke Lee8bb62472013-11-25 17:18:33 -080033
Chiao Chengbeca8562012-11-28 18:06:44 -080034import org.xmlpull.v1.XmlPullParser;
35import org.xmlpull.v1.XmlPullParserException;
36
37import java.io.IOException;
38
39/**
40 * Retrieves localized names per account type. This allows customizing texts like
41 * "All Contacts" for certain account types, but e.g. "All Friends" or "All Connections" for others.
42 */
43public class LocalizedNameResolver {
44 private static final String TAG = "LocalizedNameResolver";
45
Chiao Chengbeca8562012-11-28 18:06:44 -080046 private static final String CONTACTS_DATA_KIND = "ContactsDataKind";
47
48 /**
49 * Returns the name for All Contacts for the specified account type.
50 */
51 public static String getAllContactsName(Context context, String accountType) {
52 if (context == null) throw new IllegalArgumentException("Context must not be null");
53 if (accountType == null) return null;
54
55 return resolveAllContactsName(context, accountType);
56 }
57
58 /**
59 * Finds "All Contacts"-Name for the specified account type.
60 */
61 private static String resolveAllContactsName(Context context, String accountType) {
Ihab Awad413589f2014-07-02 14:00:28 -070062 final AccountManager am = AccountManager.get(context);
Chiao Chengbeca8562012-11-28 18:06:44 -080063
64 for (AuthenticatorDescription auth : am.getAuthenticatorTypes()) {
65 if (accountType.equals(auth.type)) {
66 return resolveAllContactsNameFromMetaData(context, auth.packageName);
67 }
68 }
69
70 return null;
71 }
72
73 /**
74 * Finds the meta-data XML containing the contacts configuration and
75 * reads the picture priority from that file.
76 */
77 private static String resolveAllContactsNameFromMetaData(Context context, String packageName) {
Jay Shraunere5ef2f72014-12-03 15:46:26 -080078 final XmlResourceParser parser = ExternalAccountType.loadContactsXml(context, packageName);
79 if (parser != null) {
80 return loadAllContactsNameFromXml(context, parser, packageName);
Chiao Chengbeca8562012-11-28 18:06:44 -080081 }
82 return null;
83 }
84
85 private static String loadAllContactsNameFromXml(Context context, XmlPullParser parser,
86 String packageName) {
87 try {
88 final AttributeSet attrs = Xml.asAttributeSet(parser);
89 int type;
90 while ((type = parser.next()) != XmlPullParser.START_TAG
91 && type != XmlPullParser.END_DOCUMENT) {
92 // Drain comments and whitespace
93 }
94
95 if (type != XmlPullParser.START_TAG) {
96 throw new IllegalStateException("No start tag found");
97 }
98
99 final int depth = parser.getDepth();
100 while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
101 && type != XmlPullParser.END_DOCUMENT) {
102 String name = parser.getName();
103 if (type == XmlPullParser.START_TAG && CONTACTS_DATA_KIND.equals(name)) {
104 final TypedArray typedArray = context.obtainStyledAttributes(attrs,
Yorke Lee8bb62472013-11-25 17:18:33 -0800105 R.styleable.ContactsDataKind);
Chiao Chengbeca8562012-11-28 18:06:44 -0800106 try {
107 // See if a string has been hardcoded directly into the xml
108 final String nonResourceString = typedArray.getNonResourceString(
Yorke Lee8bb62472013-11-25 17:18:33 -0800109 R.styleable.ContactsDataKind_android_allContactsName);
Chiao Chengbeca8562012-11-28 18:06:44 -0800110 if (nonResourceString != null) {
111 return nonResourceString;
112 }
113
114 // See if a resource is referenced. We can't rely on getString
115 // to automatically resolve it as the resource lives in a different package
116 int id = typedArray.getResourceId(
Yorke Lee8bb62472013-11-25 17:18:33 -0800117 R.styleable.ContactsDataKind_android_allContactsName, 0);
Chiao Chengbeca8562012-11-28 18:06:44 -0800118 if (id == 0) return null;
119
120 // Resolve the resource Id
121 final PackageManager packageManager = context.getPackageManager();
122 final Resources resources;
123 try {
124 resources = packageManager.getResourcesForApplication(packageName);
125 } catch (NameNotFoundException e) {
126 return null;
127 }
128 try {
129 return resources.getString(id);
130 } catch (NotFoundException e) {
131 return null;
132 }
133 } finally {
134 typedArray.recycle();
135 }
136 }
137 }
138 return null;
139 } catch (XmlPullParserException e) {
140 throw new IllegalStateException("Problem reading XML", e);
141 } catch (IOException e) {
142 throw new IllegalStateException("Problem reading XML", e);
143 }
144 }
145}