1 /*
2  * Copyright (C) 2008 Esmertec AG.
3  * Copyright (C) 2008 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mms;
19 
20 import android.app.Application;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.res.Configuration;
24 import android.drm.DrmManagerClient;
25 import android.location.Country;
26 import android.location.CountryDetector;
27 import android.location.CountryListener;
28 import android.os.StrictMode;
29 import android.preference.PreferenceManager;
30 import android.provider.SearchRecentSuggestions;
31 import android.telephony.TelephonyManager;
32 import android.util.Log;
33 
34 import com.android.mms.data.Contact;
35 import com.android.mms.data.Conversation;
36 import com.android.mms.layout.LayoutManager;
37 import com.android.mms.transaction.MessagingNotification;
38 import com.android.mms.transaction.MmsSystemEventReceiver;
39 import com.android.mms.transaction.SmsReceiver;
40 import com.android.mms.transaction.SmsReceiverService;
41 import com.android.mms.util.DownloadManager;
42 import com.android.mms.util.DraftCache;
43 import com.android.mms.util.PduLoaderManager;
44 import com.android.mms.util.RateController;
45 import com.android.mms.util.ThumbnailManager;
46 
47 import java.util.Locale;
48 
49 public class MmsApp extends Application {
50     public static final String LOG_TAG = LogTag.TAG;
51 
52     private SearchRecentSuggestions mRecentSuggestions;
53     private TelephonyManager mTelephonyManager;
54     private CountryDetector mCountryDetector;
55     private CountryListener mCountryListener;
56     private String mCountryIso;
57     private static MmsApp sMmsApp = null;
58     private PduLoaderManager mPduLoaderManager;
59     private ThumbnailManager mThumbnailManager;
60     private DrmManagerClient mDrmManagerClient;
61 
62     @Override
onCreate()63     public void onCreate() {
64         super.onCreate();
65 
66         if (Log.isLoggable(LogTag.STRICT_MODE_TAG, Log.DEBUG)) {
67             // Log tag for enabling/disabling StrictMode violation log. This will dump a stack
68             // in the log that shows the StrictMode violator.
69             // To enable: adb shell setprop log.tag.Mms:strictmode DEBUG
70             StrictMode.setThreadPolicy(
71                     new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
72         }
73 
74         sMmsApp = this;
75 
76         // Load the default preference values
77         PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
78 
79         // Figure out the country *before* loading contacts and formatting numbers
80         mCountryDetector = (CountryDetector) getSystemService(Context.COUNTRY_DETECTOR);
81         mCountryListener = new CountryListener() {
82             @Override
83             public synchronized void onCountryDetected(Country country) {
84                 mCountryIso = country.getCountryIso();
85             }
86         };
87         mCountryDetector.addCountryListener(mCountryListener, getMainLooper());
88 
89         Context context = getApplicationContext();
90         mPduLoaderManager = new PduLoaderManager(context);
91         mThumbnailManager = new ThumbnailManager(context);
92 
93         MmsConfig.init(this);
94         Contact.init(this);
95         DraftCache.init(this);
96         Conversation.init(this);
97         DownloadManager.init(this);
98         RateController.init(this);
99         LayoutManager.init(this);
100         MessagingNotification.init(this);
101 
102         activePendingMessages();
103     }
104 
105     /**
106      * Try to process all pending messages(which were interrupted by user, OOM, Mms crashing,
107      * etc...) when Mms app is (re)launched.
108      */
activePendingMessages()109     private void activePendingMessages() {
110         // For Mms: try to process all pending transactions if possible
111         MmsSystemEventReceiver.wakeUpService(this);
112 
113         // For Sms: retry to send smses in outbox and queued box
114         sendBroadcast(new Intent(SmsReceiverService.ACTION_SEND_INACTIVE_MESSAGE,
115                 null,
116                 this,
117                 SmsReceiver.class));
118     }
119 
getApplication()120     synchronized public static MmsApp getApplication() {
121         return sMmsApp;
122     }
123 
124     @Override
onTerminate()125     public void onTerminate() {
126         mCountryDetector.removeCountryListener(mCountryListener);
127     }
128 
129     @Override
onLowMemory()130     public void onLowMemory() {
131         super.onLowMemory();
132 
133         mPduLoaderManager.onLowMemory();
134         mThumbnailManager.onLowMemory();
135     }
136 
getPduLoaderManager()137     public PduLoaderManager getPduLoaderManager() {
138         return mPduLoaderManager;
139     }
140 
getThumbnailManager()141     public ThumbnailManager getThumbnailManager() {
142         return mThumbnailManager;
143     }
144 
145     @Override
onConfigurationChanged(Configuration newConfig)146     public void onConfigurationChanged(Configuration newConfig) {
147         LayoutManager.getInstance().onConfigurationChanged(newConfig);
148     }
149 
150     /**
151      * @return Returns the TelephonyManager.
152      */
getTelephonyManager()153     public TelephonyManager getTelephonyManager() {
154         if (mTelephonyManager == null) {
155             mTelephonyManager = (TelephonyManager)getApplicationContext()
156                     .getSystemService(Context.TELEPHONY_SERVICE);
157         }
158         return mTelephonyManager;
159     }
160 
161     /**
162      * Returns the content provider wrapper that allows access to recent searches.
163      * @return Returns the content provider wrapper that allows access to recent searches.
164      */
getRecentSuggestions()165     public SearchRecentSuggestions getRecentSuggestions() {
166         /*
167         if (mRecentSuggestions == null) {
168             mRecentSuggestions = new SearchRecentSuggestions(this,
169                     SuggestionsProvider.AUTHORITY, SuggestionsProvider.MODE);
170         }
171         */
172         return mRecentSuggestions;
173     }
174 
175     // This function CAN return null.
getCurrentCountryIso()176     public String getCurrentCountryIso() {
177         if (mCountryIso == null) {
178             Country country = mCountryDetector.detectCountry();
179 
180             if (country == null) {
181                 // Fallback to Locale if there are issues with CountryDetector
182                 return Locale.getDefault().getCountry();
183             }
184 
185             mCountryIso = country.getCountryIso();
186         }
187         return mCountryIso;
188     }
189 
getDrmManagerClient()190     public DrmManagerClient getDrmManagerClient() {
191         if (mDrmManagerClient == null) {
192             mDrmManagerClient = new DrmManagerClient(getApplicationContext());
193         }
194         return mDrmManagerClient;
195     }
196 
197 }
198