1/*
2 * Copyright (C) 2015 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 android.support.v7.mms;
18
19import android.content.Context;
20import android.telephony.TelephonyManager;
21import android.text.TextUtils;
22import android.util.Log;
23
24/**
25 * The default implementation of loader of UA and UAProfUrl
26 */
27class DefaultUserAgentInfoLoader implements UserAgentInfoLoader {
28    // Default values to be used as user agent info
29    private static final String DEFAULT_USER_AGENT = "Android MmsLib/1.0";
30    private static final String DEFAULT_UA_PROF_URL =
31            "http://www.gstatic.com/android/sms/mms_ua_profile.xml";
32
33    private Context mContext;
34    private boolean mLoaded;
35
36    private String mUserAgent;
37    private String mUAProfUrl;
38
39    DefaultUserAgentInfoLoader(final Context context) {
40        mContext = context;
41    }
42
43    @Override
44    public String getUserAgent() {
45        load();
46        return mUserAgent;
47    }
48
49    @Override
50    public String getUAProfUrl() {
51        load();
52        return mUAProfUrl;
53    }
54
55    private void load() {
56        if (mLoaded) {
57            return;
58        }
59        boolean didLoad = false;
60        synchronized (this) {
61            if (!mLoaded) {
62                loadLocked();
63                mLoaded = true;
64                didLoad = true;
65            }
66        }
67        if (didLoad) {
68            Log.i(MmsService.TAG, "Loaded user agent info: "
69                    + "UA=" + mUserAgent + ", UAProfUrl=" + mUAProfUrl);
70        }
71    }
72
73    private void loadLocked() {
74        if (Utils.hasUserAgentApi()) {
75            // load the MMS User agent and UaProfUrl from TelephonyManager APIs
76            final TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
77                    Context.TELEPHONY_SERVICE);
78            mUserAgent = telephonyManager.getMmsUserAgent();
79            mUAProfUrl = telephonyManager.getMmsUAProfUrl();
80        }
81        if (TextUtils.isEmpty(mUserAgent)) {
82            mUserAgent = DEFAULT_USER_AGENT;
83        }
84        if (TextUtils.isEmpty(mUAProfUrl)) {
85            mUAProfUrl = DEFAULT_UA_PROF_URL;
86        }
87    }
88}
89