2014-07-25 22:14:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-01-11 23:37:51 +00:00
|
|
|
import android.os.AsyncTask;
|
2017-02-14 06:55:06 +00:00
|
|
|
import android.os.Build;
|
2015-05-21 19:30:18 +00:00
|
|
|
import android.os.StrictMode;
|
|
|
|
import android.os.StrictMode.ThreadPolicy;
|
|
|
|
import android.os.StrictMode.VmPolicy;
|
2017-01-08 17:43:43 +00:00
|
|
|
import android.support.multidex.MultiDexApplication;
|
2017-01-11 23:37:51 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.google.android.gms.security.ProviderInstaller;
|
2015-05-21 19:30:18 +00:00
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.PRNGFixes;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.AxolotlStorageModule;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2015-09-30 23:19:50 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.RedPhoneCommunicationModule;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.SignalCommunicationModule;
|
2016-03-12 01:07:22 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob;
|
2014-11-12 05:11:57 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.GcmRefreshJob;
|
|
|
|
import org.thoughtcrime.securesms.jobs.persistence.EncryptingJobSerializer;
|
2014-11-14 23:44:49 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirementProvider;
|
2015-08-24 22:24:31 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MediaNetworkRequirementProvider;
|
2014-11-14 23:44:49 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.ServiceRequirementProvider;
|
2016-12-30 04:54:05 +00:00
|
|
|
import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
|
2017-01-06 17:19:58 +00:00
|
|
|
import org.thoughtcrime.securesms.service.DirectoryRefreshListener;
|
2016-08-16 03:23:56 +00:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2017-01-06 17:19:58 +00:00
|
|
|
import org.thoughtcrime.securesms.service.RotateSignedPreKeyListener;
|
2017-02-26 22:36:43 +00:00
|
|
|
import org.thoughtcrime.securesms.service.UpdateApkRefreshListener;
|
2014-07-25 22:14:29 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.webrtc.PeerConnectionFactory;
|
2014-08-04 23:15:13 +00:00
|
|
|
import org.whispersystems.jobqueue.JobManager;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.whispersystems.jobqueue.dependencies.DependencyInjector;
|
2014-11-14 23:44:49 +00:00
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirementProvider;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider;
|
|
|
|
import org.whispersystems.libsignal.util.AndroidSignalProtocolLogger;
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import dagger.ObjectGraph;
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
/**
|
|
|
|
* Will be called once when the TextSecure process is created.
|
|
|
|
*
|
|
|
|
* We're using this as an insertion point to patch up the Android PRNG disaster,
|
|
|
|
* to initialize the job manager, and to check for GCM registration freshness.
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
2017-01-08 17:43:43 +00:00
|
|
|
public class ApplicationContext extends MultiDexApplication implements DependencyInjector {
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2017-01-11 23:37:51 +00:00
|
|
|
private static final String TAG = ApplicationContext.class.getName();
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
private ExpiringMessageManager expiringMessageManager;
|
|
|
|
private JobManager jobManager;
|
|
|
|
private ObjectGraph objectGraph;
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2015-08-24 22:24:31 +00:00
|
|
|
private MediaNetworkRequirementProvider mediaNetworkRequirementProvider = new MediaNetworkRequirementProvider();
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
public static ApplicationContext getInstance(Context context) {
|
|
|
|
return (ApplicationContext)context.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2015-05-21 19:30:18 +00:00
|
|
|
super.onCreate();
|
|
|
|
initializeDeveloperBuild();
|
2014-07-25 22:14:29 +00:00
|
|
|
initializeRandomNumberFix();
|
2015-03-02 16:25:19 +00:00
|
|
|
initializeLogging();
|
2014-11-12 03:57:53 +00:00
|
|
|
initializeDependencyInjection();
|
2014-07-25 22:14:29 +00:00
|
|
|
initializeJobManager();
|
2016-08-16 03:23:56 +00:00
|
|
|
initializeExpiringMessageManager();
|
2014-07-25 22:14:29 +00:00
|
|
|
initializeGcmCheck();
|
2016-03-12 01:07:22 +00:00
|
|
|
initializeSignedPreKeyCheck();
|
2017-01-06 17:19:58 +00:00
|
|
|
initializePeriodicTasks();
|
2017-01-11 23:37:51 +00:00
|
|
|
initializeCircumvention();
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-02-14 06:55:06 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
PeerConnectionFactory.initializeAndroidGlobals(this, true, true, true);
|
|
|
|
}
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
@Override
|
|
|
|
public void injectDependencies(Object object) {
|
|
|
|
if (object instanceof InjectableType) {
|
|
|
|
objectGraph.inject(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
public JobManager getJobManager() {
|
|
|
|
return jobManager;
|
|
|
|
}
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
public ExpiringMessageManager getExpiringMessageManager() {
|
|
|
|
return expiringMessageManager;
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:30:18 +00:00
|
|
|
private void initializeDeveloperBuild() {
|
|
|
|
if (BuildConfig.DEV_BUILD) {
|
|
|
|
StrictMode.setThreadPolicy(new ThreadPolicy.Builder().detectAll()
|
|
|
|
.penaltyLog()
|
|
|
|
.build());
|
|
|
|
StrictMode.setVmPolicy(new VmPolicy.Builder().detectAll().penaltyLog().build());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
private void initializeRandomNumberFix() {
|
|
|
|
PRNGFixes.apply();
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:25:19 +00:00
|
|
|
private void initializeLogging() {
|
2016-03-23 17:34:41 +00:00
|
|
|
SignalProtocolLoggerProvider.setProvider(new AndroidSignalProtocolLogger());
|
2015-03-02 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
private void initializeJobManager() {
|
2014-11-12 05:11:57 +00:00
|
|
|
this.jobManager = JobManager.newBuilder(this)
|
|
|
|
.withName("TextSecureJobs")
|
|
|
|
.withDependencyInjector(this)
|
2014-11-17 00:15:12 +00:00
|
|
|
.withJobSerializer(new EncryptingJobSerializer())
|
2014-11-14 23:44:49 +00:00
|
|
|
.withRequirementProviders(new MasterSecretRequirementProvider(this),
|
|
|
|
new ServiceRequirementProvider(this),
|
2015-08-24 22:24:31 +00:00
|
|
|
new NetworkRequirementProvider(this),
|
|
|
|
mediaNetworkRequirementProvider)
|
2014-11-12 05:11:57 +00:00
|
|
|
.withConsumerThreads(5)
|
|
|
|
.build();
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:31 +00:00
|
|
|
public void notifyMediaControlEvent() {
|
|
|
|
mediaNetworkRequirementProvider.notifyMediaControlEvent();
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
private void initializeDependencyInjection() {
|
2016-12-30 04:54:05 +00:00
|
|
|
this.objectGraph = ObjectGraph.create(new SignalCommunicationModule(this, new SignalServiceNetworkAccess(this)),
|
2015-09-30 23:19:50 +00:00
|
|
|
new RedPhoneCommunicationModule(this),
|
2014-11-12 03:57:53 +00:00
|
|
|
new AxolotlStorageModule(this));
|
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
private void initializeGcmCheck() {
|
|
|
|
if (TextSecurePreferences.isPushRegistered(this) &&
|
|
|
|
TextSecurePreferences.getGcmRegistrationId(this) == null)
|
|
|
|
{
|
2014-08-04 23:15:13 +00:00
|
|
|
this.jobManager.add(new GcmRefreshJob(this));
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:07:22 +00:00
|
|
|
private void initializeSignedPreKeyCheck() {
|
|
|
|
if (!TextSecurePreferences.isSignedPreKeyRegistered(this)) {
|
|
|
|
jobManager.add(new CreateSignedPreKeyJob(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
private void initializeExpiringMessageManager() {
|
|
|
|
this.expiringMessageManager = new ExpiringMessageManager(this);
|
|
|
|
}
|
|
|
|
|
2017-01-06 17:19:58 +00:00
|
|
|
private void initializePeriodicTasks() {
|
|
|
|
RotateSignedPreKeyListener.schedule(this);
|
|
|
|
DirectoryRefreshListener.schedule(this);
|
2017-02-26 22:36:43 +00:00
|
|
|
|
|
|
|
if (BuildConfig.PLAY_STORE_DISABLED) {
|
|
|
|
UpdateApkRefreshListener.schedule(this);
|
|
|
|
}
|
2017-01-06 17:19:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:37:51 +00:00
|
|
|
private void initializeCircumvention() {
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
if (new SignalServiceNetworkAccess(ApplicationContext.this).isCensored(ApplicationContext.this)) {
|
|
|
|
try {
|
|
|
|
ProviderInstaller.installIfNeeded(ApplicationContext.this);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
Log.w(TAG, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|