Skip to content

Commit

Permalink
BraveNewPipeLegacy: use getApplicationContext to have a static variab…
Browse files Browse the repository at this point in the history
…le inside BraveApp

In the App.java from main source set there is the static method App.getApp() from which
many parts of the application get ApplicationContext. But as the class App inherits from
BraveApp and sets the app variable in onCreate() only after the super.onCreate()
from BraveApp is called. Therefore App.getApp() has not yet an initialized variable
thus we need another way to get the ApplicationContext
  • Loading branch information
evermind-zz committed Apr 25, 2024
1 parent 3df1047 commit db53faa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions app/src/braveLegacy/java/org/schabi/newpipe/BraveApp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.schabi.newpipe;

import android.content.Context;
import android.os.Build;

import org.conscrypt.Conscrypt;
Expand All @@ -10,15 +11,34 @@
import androidx.multidex.MultiDexApplication;

public class BraveApp extends MultiDexApplication {
private static Context appContext;

@Override
public void onCreate() {
super.onCreate();
Security.insertProviderAt(Conscrypt.newProvider(), 1);
appContext = getApplicationContext();

// enable TLS1.2/1.3 for <=kitkat devices, to fix download and play for
// media.ccc.de, rumble, soundcloud, peertube sources
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
BraveTLSSocketFactory.setAsDefault();
}
}

/**
* Get the application context.
* <p>
* In the {@link App} from main source set there is the static method {@link App#getApp()}
* from which many parts of the application get ApplicationContext. But as the class
* {@link App} inherits from BraveApp and sets the app variable in {@link @App#onCreate()}
* only after the {@link BraveApp#onCreate()} is called. Therefore {@link App#getApp()}
* has not yet an initialized return valule thus we need another way to get the
* ApplicationContext
*
* @return the application context
*/
public static Context getAppContext() {
return appContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.util.Log;

import org.schabi.newpipe.App;
import org.schabi.newpipe.BraveApp;
import org.schabi.newpipe.BraveTag;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -121,7 +121,7 @@ private Certificate readCertificateFromFile(
final String rawFile)
throws IOException, CertificateException {

final Context context = App.getApp().getApplicationContext();
final Context context = BraveApp.getAppContext();
final InputStream inputStream = context.getResources().openRawResource(
context.getResources().getIdentifier(rawFile,
"raw", context.getPackageName()));
Expand Down

0 comments on commit db53faa

Please sign in to comment.