Home | History | Annotate | Download | only in sms
      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 
     17 package com.android.messaging.sms;
     18 
     19 import android.content.Context;
     20 import android.support.v7.mms.UserAgentInfoLoader;
     21 import android.telephony.TelephonyManager;
     22 import android.text.TextUtils;
     23 
     24 import com.android.messaging.util.BugleGservices;
     25 import com.android.messaging.util.BugleGservicesKeys;
     26 import com.android.messaging.util.LogUtil;
     27 import com.android.messaging.util.OsUtil;
     28 import com.android.messaging.util.VersionUtil;
     29 
     30 /**
     31  * User agent and UA profile URL loader
     32  */
     33 public class BugleUserAgentInfoLoader implements UserAgentInfoLoader {
     34     private static final String DEFAULT_USER_AGENT_PREFIX = "Bugle/";
     35 
     36     private Context mContext;
     37     private boolean mLoaded;
     38 
     39     private String mUserAgent;
     40     private String mUAProfUrl;
     41 
     42     public BugleUserAgentInfoLoader(final Context context) {
     43         mContext = context;
     44     }
     45 
     46     @Override
     47     public String getUserAgent() {
     48         load();
     49         return mUserAgent;
     50     }
     51 
     52     @Override
     53     public String getUAProfUrl() {
     54         load();
     55         return mUAProfUrl;
     56     }
     57 
     58     private void load() {
     59         if (mLoaded) {
     60             return;
     61         }
     62         boolean didLoad = false;
     63         synchronized (this) {
     64             if (!mLoaded) {
     65                 loadLocked();
     66                 mLoaded = true;
     67                 didLoad = true;
     68             }
     69         }
     70         if (didLoad) {
     71             LogUtil.i(LogUtil.BUGLE_TAG, "Loaded user agent info: "
     72                     + "UA=" + mUserAgent + ", UAProfUrl=" + mUAProfUrl);
     73         }
     74     }
     75 
     76     private void loadLocked() {
     77         if (OsUtil.isAtLeastKLP()) {
     78             // load the MMS User agent and UaProfUrl from TelephonyManager APIs
     79             final TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
     80                     Context.TELEPHONY_SERVICE);
     81             mUserAgent = telephonyManager.getMmsUserAgent();
     82             mUAProfUrl = telephonyManager.getMmsUAProfUrl();
     83         }
     84         // if user agent string isn't set, use the format "Bugle/<app_version>".
     85         if (TextUtils.isEmpty(mUserAgent)) {
     86             final String simpleVersionName = VersionUtil.getInstance(mContext).getSimpleName();
     87             mUserAgent = DEFAULT_USER_AGENT_PREFIX + simpleVersionName;
     88         }
     89         // if the UAProfUrl isn't set, get it from Gservices
     90         if (TextUtils.isEmpty(mUAProfUrl)) {
     91             mUAProfUrl = BugleGservices.get().getString(
     92                     BugleGservicesKeys.MMS_UA_PROFILE_URL,
     93                     BugleGservicesKeys.MMS_UA_PROFILE_URL_DEFAULT);
     94         }
     95     }
     96 }
     97