Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable-3.29] Fix Wrong Context Usage #13098

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ android {
testInstrumentationRunnerArgument "TEST_SERVER_PASSWORD", "${NC_TEST_SERVER_PASSWORD}"
testInstrumentationRunnerArguments disableAnalytics: 'true'

multiDexEnabled true

versionCode versionMajor * 10000000 + versionMinor * 10000 + versionPatch * 100 + versionBuild

if (versionBuild > 89) {
Expand Down Expand Up @@ -264,8 +262,6 @@ android {
}

dependencies {
// dependencies for app building
implementation 'androidx.multidex:multidex:2.0.1'
implementation("com.github.nextcloud:android-library:$androidLibraryVersion") {
exclude group: 'org.ogce', module: 'xpp3' // unused in Android and brings wrong Junit version
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.MainApp;
Expand Down Expand Up @@ -52,10 +53,10 @@ public class UserAccountManagerImpl implements UserAccountManager {
private static final String PREF_SELECT_OC_ACCOUNT = "select_oc_account";

private Context context;
private AccountManager accountManager;
private final AccountManager accountManager;

public static UserAccountManagerImpl fromContext(Context context) {
AccountManager am = (AccountManager)context.getSystemService(Context.ACCOUNT_SERVICE);
AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
return new UserAccountManagerImpl(context, am);
}

Expand Down Expand Up @@ -181,7 +182,12 @@ private User createUserFromAccount(@Nullable Account account) {
return null;
}

OwnCloudAccount ownCloudAccount = null;
if (context == null) {
Log_OC.d(TAG, "Context is null MainApp.getAppContext() used");
context = MainApp.getAppContext();
}

OwnCloudAccount ownCloudAccount;
try {
ownCloudAccount = new OwnCloudAccount(account, context);
} catch (AccountUtils.AccountNotFoundException ex) {
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.conscrypt.Conscrypt;
import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
Expand All @@ -108,7 +109,6 @@
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.multidex.MultiDexApplication;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;
Expand All @@ -123,14 +123,14 @@
* Main Application of the project.
* Contains methods to build the "static" strings. These strings were before constants in different classes.
*/
public class MainApp extends MultiDexApplication implements HasAndroidInjector {
public class MainApp extends Application implements HasAndroidInjector {
public static final OwnCloudVersion OUTDATED_SERVER_VERSION = NextcloudVersion.nextcloud_26;
public static final OwnCloudVersion MINIMUM_SUPPORTED_SERVER_VERSION = OwnCloudVersion.nextcloud_16;

private static final String TAG = MainApp.class.getSimpleName();
public static final String DOT = ".";

private static Context mContext;
private static WeakReference<Context> appContext;

private static String storagePath;

Expand Down Expand Up @@ -197,7 +197,7 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
* Temporary hack
*/
private static void initGlobalContext(Context context) {
mContext = context;
appContext = new WeakReference<>(context);
}

/**
Expand Down Expand Up @@ -448,7 +448,7 @@ private void initSecurityKeyManager() {
}

public static void initContactsBackup(UserAccountManager accountManager, BackgroundJobManager backgroundJobManager) {
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl(mContext);
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl(appContext.get());
List<User> users = accountManager.getAllUsers();
for (User user : users) {
if (arbitraryDataProvider.getBooleanValue(user, PREFERENCE_CONTACTS_AUTOMATIC_BACKUP)) {
Expand Down Expand Up @@ -565,7 +565,7 @@ public static void initSyncOperations(
preferences.setAutoUploadInit(true);
}

FilesSyncHelper.scheduleFilesSyncIfNeeded(mContext, backgroundJobManager);
FilesSyncHelper.scheduleFilesSyncIfNeeded(getAppContext(), backgroundJobManager);
FilesSyncHelper.restartJobsIfNeeded(
uploadsStorageManager,
accountManager,
Expand Down Expand Up @@ -661,11 +661,11 @@ public static String string(int id, Object args) {
}

public static Context getAppContext() {
return MainApp.mContext;
return MainApp.appContext.get();
}

public static void setAppContext(Context context) {
MainApp.mContext = context;
MainApp.appContext = new WeakReference<>(context);
}

public static String getStoragePath() {
Expand Down Expand Up @@ -787,7 +787,7 @@ private static void updateAutoUploadEntries(Clock clock) {
if (!preferences.isAutoUploadPathsUpdateEnabled()) {
SyncedFolderProvider syncedFolderProvider =
new SyncedFolderProvider(MainApp.getAppContext().getContentResolver(), preferences, clock);
syncedFolderProvider.updateAutoUploadPaths(mContext);
syncedFolderProvider.updateAutoUploadPaths(appContext.get());
}
}

Expand Down
36 changes: 16 additions & 20 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5151,16 +5151,14 @@
<sha256 value="c29795ee883fc3364b2f16be5b9246b927271b961214f1a661b2caa2f42459a8" origin="Generated by Gradle" reason="Artifact is not signed"/>
</artifact>
</component>
<component group="com.github.nextcloud" name="android-library" version="2.19.0">
<artifact name="android-library-2.19.0.aar">
<sha256 value="638229c571aea3f28168a84323307806c34ef6e90459242ff115add9c6ca4a67"
origin="Generated by Gradle" reason="Artifact is not signed" />
</artifact>
<artifact name="android-library-2.19.0.module">
<sha256 value="828a431c00178ed0a0e7d51b7959f785be8266869dcb539dacaf0fc909b92b11"
origin="Generated by Gradle" reason="Artifact is not signed" />
</artifact>
</component>
<component group="com.github.nextcloud" name="android-library" version="2.19.0">
<artifact name="android-library-2.19.0.aar">
<sha256 value="638229c571aea3f28168a84323307806c34ef6e90459242ff115add9c6ca4a67" origin="Generated by Gradle" reason="Artifact is not signed"/>
</artifact>
<artifact name="android-library-2.19.0.module">
<sha256 value="828a431c00178ed0a0e7d51b7959f785be8266869dcb539dacaf0fc909b92b11" origin="Generated by Gradle" reason="Artifact is not signed"/>
</artifact>
</component>
<component group="com.github.nextcloud" name="android-library" version="2b1da4cb14e2cd4b79e231b0be54e0bae699f143">
<artifact name="android-library-2b1da4cb14e2cd4b79e231b0be54e0bae699f143.aar">
<sha256 value="bdc44e874f1e14338213ae5723e71710940a31416ff1c52c9eb2f282e5d3f29a" origin="Generated by Gradle" reason="Artifact is not signed"/>
Expand All @@ -5169,16 +5167,14 @@
<sha256 value="b4f19aae9d8687651cf8f760cb89be155cfa0a9aed818cb50cfbcb15f36c9f2d" origin="Generated by Gradle" reason="Artifact is not signed"/>
</artifact>
</component>
<component group="com.github.nextcloud" name="android-library" version="3c0c530e87">
<artifact name="android-library-3c0c530e87.aar">
<sha256 value="638229c571aea3f28168a84323307806c34ef6e90459242ff115add9c6ca4a67"
origin="Generated by Gradle" reason="Artifact is not signed" />
</artifact>
<artifact name="android-library-3c0c530e87.module">
<sha256 value="798192237df40523ae089fc4ebf3127fae4aef3d31e5a6c9b264b4ecd9baa738"
origin="Generated by Gradle" reason="Artifact is not signed" />
</artifact>
</component>
<component group="com.github.nextcloud" name="android-library" version="3c0c530e87">
<artifact name="android-library-3c0c530e87.aar">
<sha256 value="638229c571aea3f28168a84323307806c34ef6e90459242ff115add9c6ca4a67" origin="Generated by Gradle" reason="Artifact is not signed"/>
</artifact>
<artifact name="android-library-3c0c530e87.module">
<sha256 value="798192237df40523ae089fc4ebf3127fae4aef3d31e5a6c9b264b4ecd9baa738" origin="Generated by Gradle" reason="Artifact is not signed"/>
</artifact>
</component>
<component group="com.github.nextcloud" name="android-library" version="5f92d92c490d2d9039bfd392cf16c61a37738646">
<artifact name="android-library-5f92d92c490d2d9039bfd392cf16c61a37738646.aar">
<sha256 value="adec67393e6d6a05d1d96ed4e4dd2e675999e43ca544ea069cee1dbac4d3132c" origin="Generated by Gradle" reason="Artifact is not signed"/>
Expand Down
2 changes: 1 addition & 1 deletion scripts/analysis/lint-results.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DO NOT TOUCH; GENERATED BY DRONE
<span class="mdl-layout-title">Lint Report: 3 errors and 71 warnings</span>
<span class="mdl-layout-title">Lint Report: 3 errors and 70 warnings</span>
Loading