[android] Replace mApplication null checks with assertions

- and other minor changes as per code review

Co-authored-by: Alexander Borsuk <170263+biodranik@users.noreply.github.com>
Signed-off-by: Konstantin Pastbin <konstantin.pastbin@gmail.com>
This commit is contained in:
Konstantin Pastbin 2022-06-06 00:01:06 +03:00 committed by Viktor Govako
parent 9e2b934699
commit 8633ed6ebd
4 changed files with 22 additions and 20 deletions

View file

@ -485,7 +485,7 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment
if (!LoggerFactory.INSTANCE.setFileLoggingEnabled((Boolean) newValue))
{
// It's a very rare condition when debugging, so we can do without translation.
Utils.showSnackbar(getView(), "Can't create a logs folder!");
Utils.showSnackbar(getView(), "ERROR: Can't create a logs folder!");
return false;
}
return true;

View file

@ -7,7 +7,6 @@ import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -80,7 +79,9 @@ public class StorageUtils
final File directory = new File(path);
if (!directory.exists() && !directory.mkdirs())
{
LOGGER.e(TAG, "Can't create directory " + path);
final String errMsg = "Can't create directory " + path;
LOGGER.e(TAG, errMsg);
CrashlyticsUtils.INSTANCE.logException(new IOException(errMsg));
return false;
}
return true;

View file

@ -818,7 +818,7 @@ public class Utils
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { mEmail });
intent.putExtra(Intent.EXTRA_SUBJECT, "[" + BuildConfig.VERSION_NAME + "] " + mSubject);
//TODO: if zipping logs failed send a short text attachment with system info and logs zipping error/stacktrace.
// TODO: Send a short text attachment with system info and logs if zipping logs failed
if (success)
{
final Uri uri = StorageUtils.getUriForFilePath(activity, zipPath);
@ -841,7 +841,7 @@ public class Utils
}
catch (ActivityNotFoundException e)
{
LOGGER.w("No activities found which can handle sending a support message.", e);
LOGGER.w(TAG, "No activities found which can handle sending a support message.", e);
}
});
}

View file

@ -93,12 +93,12 @@ public class LoggerFactory
*
* @return logs folder path, null if it can't be created
*
* NOTE: throws a NullPointerException if initFileLogging() was not called before.
* NOTE: initFileLogging() must be called before.
*/
@Nullable
public synchronized String ensureLogsFolder()
{
Objects.requireNonNull(mApplication);
assert mApplication != null : "mApplication must be initialized first by calling initFileLogging()";
if (mLogsFolder != null && createWritableDir(mLogsFolder))
return mLogsFolder;
@ -106,9 +106,7 @@ public class LoggerFactory
mLogsFolder = null;
mLogsFolder = createLogsFolder(mApplication.getExternalFilesDir(null));
if (mLogsFolder == null)
{
mLogsFolder = createLogsFolder(mApplication.getFilesDir());
}
if (mLogsFolder == null)
{
@ -167,13 +165,15 @@ public class LoggerFactory
/**
* Returns false if file logging can't be enabled.
* NOTE: Throws a NullPointerException if initFileLogging() was not called before.
*
* NOTE: initFileLogging() must be called before.
*/
public boolean setFileLoggingEnabled(boolean enabled)
{
Objects.requireNonNull(mApplication);
assert mApplication != null : "mApplication must be initialized first by calling initFileLogging()";
if (isFileLoggingEnabled != enabled)
{
if (enabled && ensureLogsFolder() == null)
{
Log.e(TAG, "Can't enable file logging: there is no logs folder.");
@ -181,6 +181,7 @@ public class LoggerFactory
}
else
switchFileLoggingEnabled(enabled);
}
return true;
}
@ -204,11 +205,11 @@ public class LoggerFactory
}
/**
* Throws a NullPointerException if initFileLogging() was not called before.
* NOTE: initFileLogging() must be called before.
*/
public synchronized void zipLogs(@NonNull OnZipCompletedListener listener)
{
Objects.requireNonNull(mApplication);
assert mApplication != null : "mApplication must be initialized first by calling initFileLogging()";
if (ensureLogsFolder() == null)
{
@ -269,17 +270,17 @@ public class LoggerFactory
}
/**
* Throws a NullPointerException if initFileLogging() was not called before.
* NOTE: initFileLogging() must be called before.
*/
public void writeSystemInformation(@NonNull final FileWriter fw) throws IOException
{
Objects.requireNonNull(mApplication);
assert mApplication != null : "mApplication must be initialized first by calling initFileLogging()";
fw.write("Android version: " + Build.VERSION.SDK_INT + "\n");
fw.write("Device: " + Utils.getFullDeviceModel() + "\n");
fw.write("App version: " + BuildConfig.APPLICATION_ID + " " + BuildConfig.VERSION_NAME + "\n");
fw.write("Locale : " + Locale.getDefault());
fw.write("\nNetworks : ");
fw.write("Android version: " + Build.VERSION.SDK_INT);
fw.write("\nDevice: " + Utils.getFullDeviceModel());
fw.write("\nApp version: " + BuildConfig.APPLICATION_ID + " " + BuildConfig.VERSION_NAME);
fw.write("\nLocale: " + Locale.getDefault());
fw.write("\nNetworks: ");
final ConnectivityManager manager = (ConnectivityManager) mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager != null)
// TODO: getAllNetworkInfo() is deprecated, for alternatives check