forked from organicmaps/organicmaps
[android] Unbound logger from static application
This commit is contained in:
parent
489db4e870
commit
7ac6fff4cc
8 changed files with 85 additions and 48 deletions
|
@ -127,6 +127,7 @@ public class MwmApplication extends Application
|
|||
public void onCreate()
|
||||
{
|
||||
super.onCreate();
|
||||
LoggerFactory.INSTANCE.initialize(this);
|
||||
mLogger = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
|
||||
mLogger.d(TAG, "Application is created");
|
||||
mMainLoopHandler = new Handler(getMainLooper());
|
||||
|
@ -177,9 +178,9 @@ public class MwmApplication extends Application
|
|||
|
||||
final String settingsPath = StorageUtils.getSettingsPath();
|
||||
mLogger.d(TAG, "onCreate(), setting path = " + settingsPath);
|
||||
final String filesPath = StorageUtils.getFilesPath();
|
||||
final String filesPath = StorageUtils.getFilesPath(this);
|
||||
mLogger.d(TAG, "onCreate(), files path = " + filesPath);
|
||||
final String tempPath = StorageUtils.getTempPath();
|
||||
final String tempPath = StorageUtils.getTempPath(this);
|
||||
mLogger.d(TAG, "onCreate(), temp path = " + tempPath);
|
||||
|
||||
// If platform directories are not created it means that native part of app will not be able
|
||||
|
@ -189,7 +190,7 @@ public class MwmApplication extends Application
|
|||
return;
|
||||
|
||||
// First we need initialize paths and platform to have access to settings and other components.
|
||||
nativeInitPlatform(StorageUtils.getApkPath(), StorageUtils.getStoragePath(settingsPath),
|
||||
nativeInitPlatform(StorageUtils.getApkPath(this), StorageUtils.getStoragePath(settingsPath),
|
||||
filesPath, tempPath, StorageUtils.getObbGooglePath(), BuildConfig.FLAVOR,
|
||||
BuildConfig.BUILD_TYPE, UiUtils.isTablet());
|
||||
|
||||
|
|
|
@ -423,7 +423,8 @@ public class Factory
|
|||
final String ext = getExtensionFromMime(resolver.getType(mData));
|
||||
if (ext != null)
|
||||
{
|
||||
final String filePath = StorageUtils.getTempPath() + "Attachment" + ext;
|
||||
final String filePath = StorageUtils.getTempPath(mActivity.getApplication())
|
||||
+ "Attachment" + ext;
|
||||
|
||||
File tmpFile = new File(filePath);
|
||||
output = new FileOutputStream(tmpFile);
|
||||
|
|
|
@ -16,7 +16,7 @@ class JobServiceDelegate
|
|||
mApp = app;
|
||||
}
|
||||
|
||||
public boolean onStartJob()
|
||||
boolean onStartJob()
|
||||
{
|
||||
ConnectionState.Type type = ConnectionState.requestCurrentType();
|
||||
if (type == ConnectionState.Type.WIFI)
|
||||
|
@ -31,7 +31,7 @@ class JobServiceDelegate
|
|||
ConnectivityJobScheduler.from(mApp).listen();
|
||||
}
|
||||
|
||||
public boolean onStopJob()
|
||||
boolean onStopJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.mapswithme.util;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
|
@ -11,7 +12,6 @@ import android.text.TextUtils;
|
|||
import android.util.Log;
|
||||
|
||||
import com.mapswithme.maps.BuildConfig;
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.settings.StoragePathManager;
|
||||
import com.mapswithme.util.log.Logger;
|
||||
import com.mapswithme.util.log.LoggerFactory;
|
||||
|
@ -43,12 +43,12 @@ public class StorageUtils
|
|||
* @see Context#getExternalFilesDir(String)
|
||||
*/
|
||||
@Nullable
|
||||
private static String getExternalFilesDir()
|
||||
private static String getExternalFilesDir(@NonNull Application application)
|
||||
{
|
||||
if (!isExternalStorageWritable())
|
||||
return null;
|
||||
|
||||
File dir = MwmApplication.get().getExternalFilesDir(null);
|
||||
File dir = application.getExternalFilesDir(null);
|
||||
if (dir != null)
|
||||
return dir.getAbsolutePath();
|
||||
|
||||
|
@ -62,9 +62,9 @@ public class StorageUtils
|
|||
* try to create it and all missed parent folders.
|
||||
* @return true - if folder exists, otherwise - false
|
||||
*/
|
||||
public static boolean ensureLogsFolderExistence()
|
||||
public static boolean ensureLogsFolderExistence(@NonNull Application application)
|
||||
{
|
||||
String externalDir = StorageUtils.getExternalFilesDir();
|
||||
String externalDir = StorageUtils.getExternalFilesDir(application);
|
||||
if (TextUtils.isEmpty(externalDir))
|
||||
return false;
|
||||
|
||||
|
@ -76,30 +76,30 @@ public class StorageUtils
|
|||
}
|
||||
|
||||
@Nullable
|
||||
public static String getLogsFolder()
|
||||
public static String getLogsFolder(@NonNull Application application)
|
||||
{
|
||||
if (!ensureLogsFolderExistence())
|
||||
if (!ensureLogsFolderExistence(application))
|
||||
return null;
|
||||
|
||||
String externalDir = StorageUtils.getExternalFilesDir();
|
||||
String externalDir = StorageUtils.getExternalFilesDir(application);
|
||||
return externalDir + File.separator + LOGS_FOLDER;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static String getLogsZipPath()
|
||||
static String getLogsZipPath(@NonNull Application application)
|
||||
{
|
||||
String zipFile = getExternalFilesDir() + File.separator + LOGS_FOLDER + ".zip";
|
||||
String zipFile = getExternalFilesDir(application) + File.separator + LOGS_FOLDER + ".zip";
|
||||
File file = new File(zipFile);
|
||||
return file.isFile() && file.exists() ? zipFile : null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String getApkPath()
|
||||
public static String getApkPath(@NonNull Application application)
|
||||
{
|
||||
try
|
||||
{
|
||||
return MwmApplication.get().getPackageManager().
|
||||
getApplicationInfo(BuildConfig.APPLICATION_ID, 0).sourceDir;
|
||||
return application.getPackageManager()
|
||||
.getApplicationInfo(BuildConfig.APPLICATION_ID, 0).sourceDir;
|
||||
}
|
||||
catch (final PackageManager.NameNotFoundException e)
|
||||
{
|
||||
|
@ -133,9 +133,9 @@ public class StorageUtils
|
|||
}
|
||||
|
||||
@NonNull
|
||||
public static String getFilesPath()
|
||||
public static String getFilesPath(@NonNull Application application)
|
||||
{
|
||||
final File filesDir = MwmApplication.get().getExternalFilesDir(null);
|
||||
final File filesDir = application.getExternalFilesDir(null);
|
||||
if (filesDir != null)
|
||||
return filesDir.getAbsolutePath();
|
||||
|
||||
|
@ -144,9 +144,9 @@ public class StorageUtils
|
|||
}
|
||||
|
||||
@NonNull
|
||||
public static String getTempPath()
|
||||
public static String getTempPath(@NonNull Application application)
|
||||
{
|
||||
final File cacheDir = MwmApplication.get().getExternalCacheDir();
|
||||
final File cacheDir = application.getExternalCacheDir();
|
||||
if (cacheDir != null)
|
||||
return cacheDir.getAbsolutePath();
|
||||
|
||||
|
@ -179,7 +179,7 @@ public class StorageUtils
|
|||
return true;
|
||||
}
|
||||
|
||||
public static long getFileSize(@NonNull String path)
|
||||
static long getFileSize(@NonNull String path)
|
||||
{
|
||||
File file = new File(path);
|
||||
return file.length();
|
||||
|
|
|
@ -13,7 +13,6 @@ import android.net.Uri;
|
|||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.DimenRes;
|
||||
import android.support.annotation.NonNull;
|
||||
|
@ -662,7 +661,7 @@ public class Utils
|
|||
intent.putExtra(Intent.EXTRA_SUBJECT, "[" + BuildConfig.VERSION_NAME + "] " + mSubject);
|
||||
if (success)
|
||||
{
|
||||
String logsZipFile = StorageUtils.getLogsZipPath();
|
||||
String logsZipFile = StorageUtils.getLogsZipPath(activity.getApplication());
|
||||
if (!TextUtils.isEmpty(logsZipFile))
|
||||
{
|
||||
Uri uri = StorageUtils.getUriForFilePath(activity, logsZipFile);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.mapswithme.util.log;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.location.LocationManager;
|
||||
import android.net.ConnectivityManager;
|
||||
|
@ -9,7 +10,6 @@ import android.support.annotation.NonNull;
|
|||
import android.util.Log;
|
||||
|
||||
import com.mapswithme.maps.BuildConfig;
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.util.StorageUtils;
|
||||
import com.mapswithme.util.Utils;
|
||||
import net.jcip.annotations.Immutable;
|
||||
|
@ -31,9 +31,13 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
private final String mFilePath;
|
||||
@NonNull
|
||||
private final Executor mExecutor;
|
||||
@NonNull
|
||||
private final Application mApplication;
|
||||
|
||||
FileLoggerStrategy(@NonNull String filePath, @NonNull Executor executor)
|
||||
FileLoggerStrategy(@NonNull Application application, @NonNull String filePath,
|
||||
@NonNull Executor executor)
|
||||
{
|
||||
mApplication = application;
|
||||
mFilePath = filePath;
|
||||
mExecutor = executor;
|
||||
}
|
||||
|
@ -107,7 +111,7 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
|
||||
private void write(@NonNull final String data)
|
||||
{
|
||||
mExecutor.execute(new WriteTask(mFilePath, data, Thread.currentThread().getName()));
|
||||
mExecutor.execute(new WriteTask(mApplication, mFilePath, data, Thread.currentThread().getName()));
|
||||
}
|
||||
|
||||
static class WriteTask implements Runnable
|
||||
|
@ -119,9 +123,13 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
private final String mData;
|
||||
@NonNull
|
||||
private final String mCallingThread;
|
||||
@NonNull
|
||||
private final Application mApplication;
|
||||
|
||||
private WriteTask(@NonNull String filePath, @NonNull String data, @NonNull String callingThread)
|
||||
private WriteTask(@NonNull Application application, @NonNull String filePath,
|
||||
@NonNull String data, @NonNull String callingThread)
|
||||
{
|
||||
mApplication = application;
|
||||
mFilePath = filePath;
|
||||
mData = data;
|
||||
mCallingThread = callingThread;
|
||||
|
@ -137,7 +145,7 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
if (!file.exists() || file.length() > MAX_SIZE)
|
||||
{
|
||||
fw = new FileWriter(file, false);
|
||||
writeSystemInformation(fw);
|
||||
writeSystemInformation(mApplication, fw);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -149,7 +157,7 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
catch (IOException e)
|
||||
{
|
||||
Log.e(TAG, "Failed to write the string: " + mData, e);
|
||||
Log.i(TAG, "Is logs folder existent: " + StorageUtils.ensureLogsFolderExistence());
|
||||
Log.i(TAG, "Logs folder exists: " + StorageUtils.ensureLogsFolderExistence(mApplication));
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -165,7 +173,8 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
}
|
||||
}
|
||||
|
||||
static void writeSystemInformation(FileWriter fw) throws IOException
|
||||
static void writeSystemInformation(@NonNull Application application, @NonNull FileWriter fw)
|
||||
throws IOException
|
||||
{
|
||||
fw.write("Android version: " + Build.VERSION.SDK_INT + "\n");
|
||||
fw.write("Device: " + Utils.getFullDeviceModel() + "\n");
|
||||
|
@ -173,11 +182,11 @@ class FileLoggerStrategy implements LoggerStrategy
|
|||
fw.write("Installation ID: " + Utils.getInstallationId() + "\n");
|
||||
fw.write("Locale : " + Locale.getDefault());
|
||||
fw.write("\nNetworks : ");
|
||||
final ConnectivityManager manager = (ConnectivityManager) MwmApplication.get().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
final ConnectivityManager manager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
for (NetworkInfo info : manager.getAllNetworkInfo())
|
||||
fw.write(info.toString());
|
||||
fw.write("\nLocation providers: ");
|
||||
final LocationManager locMngr = (android.location.LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE);
|
||||
final LocationManager locMngr = (android.location.LocationManager) application.getSystemService(Context.LOCATION_SERVICE);
|
||||
for (String provider: locMngr.getProviders(true))
|
||||
fw.write(provider + " ");
|
||||
fw.write("\n\n");
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.mapswithme.util.log;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
@ -16,6 +17,7 @@ import ru.mail.notify.core.utils.LogReceiver;
|
|||
|
||||
import java.io.File;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
|
@ -48,24 +50,41 @@ public class LoggerFactory
|
|||
@Nullable
|
||||
@GuardedBy("this")
|
||||
private ExecutorService mFileLoggerExecutor;
|
||||
@Nullable
|
||||
private Application mApplication;
|
||||
|
||||
private LoggerFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public void initialize(@NonNull Application application)
|
||||
{
|
||||
mApplication = application;
|
||||
}
|
||||
|
||||
public boolean isFileLoggingEnabled()
|
||||
{
|
||||
SharedPreferences prefs = MwmApplication.prefs();
|
||||
String enableLoggingKey = MwmApplication.get().getString(R.string.pref_enable_logging);
|
||||
if (mApplication == null)
|
||||
{
|
||||
if (BuildConfig.DEBUG)
|
||||
throw new IllegalStateException("Application is not created," +
|
||||
"but logger is used!");
|
||||
return false;
|
||||
}
|
||||
|
||||
SharedPreferences prefs = MwmApplication.prefs(mApplication);
|
||||
String enableLoggingKey = mApplication.getString(R.string.pref_enable_logging);
|
||||
//noinspection ConstantConditions
|
||||
return prefs.getBoolean(enableLoggingKey, BuildConfig.BUILD_TYPE.equals("beta"));
|
||||
}
|
||||
|
||||
public void setFileLoggingEnabled(boolean enabled)
|
||||
{
|
||||
Objects.requireNonNull(mApplication);
|
||||
nativeToggleCoreDebugLogs(enabled);
|
||||
SharedPreferences prefs = MwmApplication.prefs();
|
||||
SharedPreferences prefs = MwmApplication.prefs(mApplication);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
String enableLoggingKey = MwmApplication.get().getString(R.string.pref_enable_logging);
|
||||
String enableLoggingKey = mApplication.getString(R.string.pref_enable_logging);
|
||||
editor.putBoolean(enableLoggingKey, enabled).apply();
|
||||
updateLoggers();
|
||||
}
|
||||
|
@ -93,7 +112,10 @@ public class LoggerFactory
|
|||
|
||||
public synchronized void zipLogs(@Nullable OnZipCompletedListener listener)
|
||||
{
|
||||
String logsFolder = StorageUtils.getLogsFolder();
|
||||
if (mApplication == null)
|
||||
return;
|
||||
|
||||
String logsFolder = StorageUtils.getLogsFolder(mApplication);
|
||||
|
||||
if (TextUtils.isEmpty(logsFolder))
|
||||
{
|
||||
|
@ -102,7 +124,7 @@ public class LoggerFactory
|
|||
return;
|
||||
}
|
||||
|
||||
Runnable task = new ZipLogsTask(logsFolder, logsFolder + ".zip", listener);
|
||||
Runnable task = new ZipLogsTask(mApplication, logsFolder, logsFolder + ".zip", listener);
|
||||
getFileLoggerExecutor().execute(task);
|
||||
}
|
||||
|
||||
|
@ -116,12 +138,12 @@ public class LoggerFactory
|
|||
@NonNull
|
||||
private LoggerStrategy createLoggerStrategy(@NonNull Type type)
|
||||
{
|
||||
if (isFileLoggingEnabled())
|
||||
if (isFileLoggingEnabled() && mApplication != null)
|
||||
{
|
||||
nativeToggleCoreDebugLogs(true);
|
||||
String logsFolder = StorageUtils.getLogsFolder();
|
||||
String logsFolder = StorageUtils.getLogsFolder(mApplication);
|
||||
if (!TextUtils.isEmpty(logsFolder))
|
||||
return new FileLoggerStrategy(logsFolder + File.separator
|
||||
return new FileLoggerStrategy(mApplication,logsFolder + File.separator
|
||||
+ type.name().toLowerCase() + ".log", getFileLoggerExecutor());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.mapswithme.util.log;
|
||||
|
||||
import android.app.Application;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
@ -29,10 +30,14 @@ class ZipLogsTask implements Runnable
|
|||
private final String mDestPath;
|
||||
@Nullable
|
||||
private final LoggerFactory.OnZipCompletedListener mOnCompletedListener;
|
||||
@NonNull
|
||||
private final Application mApplication;
|
||||
|
||||
ZipLogsTask(@NonNull String sourcePath, @NonNull String destPath,
|
||||
ZipLogsTask(@NonNull Application application, @NonNull String sourcePath,
|
||||
@NonNull String destPath,
|
||||
@Nullable LoggerFactory.OnZipCompletedListener onCompletedListener)
|
||||
{
|
||||
mApplication = application;
|
||||
mSourcePath = sourcePath;
|
||||
mDestPath = destPath;
|
||||
mOnCompletedListener = onCompletedListener;
|
||||
|
@ -121,16 +126,16 @@ class ZipLogsTask implements Runnable
|
|||
return segments[segments.length - 1];
|
||||
}
|
||||
|
||||
private static void saveSystemLogcat()
|
||||
private void saveSystemLogcat()
|
||||
{
|
||||
String fullName = StorageUtils.getLogsFolder() + File.separator + "logcat.log";
|
||||
String fullName = StorageUtils.getLogsFolder(mApplication) + File.separator + "logcat.log";
|
||||
final File file = new File(fullName);
|
||||
InputStreamReader reader = null;
|
||||
FileWriter writer = null;
|
||||
try
|
||||
{
|
||||
writer = new FileWriter(file);
|
||||
FileLoggerStrategy.WriteTask.writeSystemInformation(writer);
|
||||
FileLoggerStrategy.WriteTask.writeSystemInformation(mApplication, writer);
|
||||
String cmd = "logcat -d -v time";
|
||||
Process process = Runtime.getRuntime().exec(cmd);
|
||||
reader = new InputStreamReader(process.getInputStream());
|
||||
|
|
Loading…
Add table
Reference in a new issue