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
18package com.android.mms;
19
20import android.app.Application;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.Configuration;
24import android.drm.DrmManagerClient;
25import android.location.Country;
26import android.location.CountryDetector;
27import android.location.CountryListener;
28import android.os.StrictMode;
29import android.preference.PreferenceManager;
30import android.provider.SearchRecentSuggestions;
31import android.telephony.TelephonyManager;
32import android.util.Log;
33
34import com.android.mms.data.Contact;
35import com.android.mms.data.Conversation;
36import com.android.mms.layout.LayoutManager;
37import com.android.mms.transaction.MessagingNotification;
38import com.android.mms.transaction.MmsSystemEventReceiver;
39import com.android.mms.transaction.SmsReceiver;
40import com.android.mms.transaction.SmsReceiverService;
41import com.android.mms.util.DownloadManager;
42import com.android.mms.util.DraftCache;
43import com.android.mms.util.PduLoaderManager;
44import com.android.mms.util.RateController;
45import com.android.mms.util.SmileyParser;
46import com.android.mms.util.ThumbnailManager;
47
48public class MmsApp extends Application {
49    public static final String LOG_TAG = "Mms";
50
51    private SearchRecentSuggestions mRecentSuggestions;
52    private TelephonyManager mTelephonyManager;
53    private CountryDetector mCountryDetector;
54    private CountryListener mCountryListener;
55    private String mCountryIso;
56    private static MmsApp sMmsApp = null;
57    private PduLoaderManager mPduLoaderManager;
58    private ThumbnailManager mThumbnailManager;
59    private DrmManagerClient mDrmManagerClient;
60
61    @Override
62    public void onCreate() {
63        super.onCreate();
64
65        if (Log.isLoggable(LogTag.STRICT_MODE_TAG, Log.DEBUG)) {
66            // Log tag for enabling/disabling StrictMode violation log. This will dump a stack
67            // in the log that shows the StrictMode violator.
68            // To enable: adb shell setprop log.tag.Mms:strictmode DEBUG
69            StrictMode.setThreadPolicy(
70                    new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
71        }
72
73        sMmsApp = this;
74
75        // Load the default preference values
76        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
77
78        // Figure out the country *before* loading contacts and formatting numbers
79        mCountryDetector = (CountryDetector) getSystemService(Context.COUNTRY_DETECTOR);
80        mCountryListener = new CountryListener() {
81            @Override
82            public synchronized void onCountryDetected(Country country) {
83                mCountryIso = country.getCountryIso();
84            }
85        };
86        mCountryDetector.addCountryListener(mCountryListener, getMainLooper());
87
88        Context context = getApplicationContext();
89        mPduLoaderManager = new PduLoaderManager(context);
90        mThumbnailManager = new ThumbnailManager(context);
91
92        MmsConfig.init(this);
93        Contact.init(this);
94        DraftCache.init(this);
95        Conversation.init(this);
96        DownloadManager.init(this);
97        RateController.init(this);
98        LayoutManager.init(this);
99        SmileyParser.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     */
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
120    synchronized public static MmsApp getApplication() {
121        return sMmsApp;
122    }
123
124    @Override
125    public void onTerminate() {
126        mCountryDetector.removeCountryListener(mCountryListener);
127    }
128
129    @Override
130    public void onLowMemory() {
131        super.onLowMemory();
132
133        mPduLoaderManager.onLowMemory();
134        mThumbnailManager.onLowMemory();
135    }
136
137    public PduLoaderManager getPduLoaderManager() {
138        return mPduLoaderManager;
139    }
140
141    public ThumbnailManager getThumbnailManager() {
142        return mThumbnailManager;
143    }
144
145    @Override
146    public void onConfigurationChanged(Configuration newConfig) {
147        LayoutManager.getInstance().onConfigurationChanged(newConfig);
148    }
149
150    /**
151     * @return Returns the TelephonyManager.
152     */
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     */
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.
176    public String getCurrentCountryIso() {
177        if (mCountryIso == null) {
178            Country country = mCountryDetector.detectCountry();
179            if (country != null) {
180                mCountryIso = country.getCountryIso();
181            }
182        }
183        return mCountryIso;
184    }
185
186    public DrmManagerClient getDrmManagerClient() {
187        if (mDrmManagerClient == null) {
188            mDrmManagerClient = new DrmManagerClient(getApplicationContext());
189        }
190        return mDrmManagerClient;
191    }
192
193}
194