3838import android .view .MenuItem ;
3939import android .view .View ;
4040import android .view .ViewGroup ;
41+ import android .webkit .WebView ;
4142import android .widget .AdapterView ;
4243import android .widget .ArrayAdapter ;
4344import android .widget .FrameLayout ;
@@ -125,7 +126,10 @@ public class MainActivity extends AppCompatActivity {
125126 private static final int ITEM_ID_ABOUT = 2 ;
126127
127128 private static final int ORDER = 0 ;
129+ public static final String KEY_IS_IN_BACKGROUND = "is_in_background" ;
128130
131+ private SharedPreferences sharedPreferences ;
132+ private SharedPreferences .Editor sharedPrefEditor ;
129133 /*//////////////////////////////////////////////////////////////////////////
130134 // Activity's LifeCycle
131135 //////////////////////////////////////////////////////////////////////////*/
@@ -140,8 +144,23 @@ protected void onCreate(final Bundle savedInstanceState) {
140144 ThemeHelper .setDayNightMode (this );
141145 ThemeHelper .setTheme (this , ServiceHelper .getSelectedServiceId (this ));
142146
147+ // Fixes text color turning black in dark/black mode:
148+ // https://github.com/TeamNewPipe/NewPipe/issues/12016
149+ // For further reference see: https://issuetracker.google.com/issues/37124582
150+ if (DeviceUtils .supportsWebView ()) {
151+ try {
152+ new WebView (this );
153+ } catch (final Throwable e ) {
154+ if (DEBUG ) {
155+ Log .e (TAG , "Failed to create WebView" , e );
156+ }
157+ }
158+ }
159+
143160 assureCorrectAppLanguage (this );
144161 super .onCreate (savedInstanceState );
162+ sharedPreferences = PreferenceManager .getDefaultSharedPreferences (this );
163+ sharedPrefEditor = sharedPreferences .edit ();
145164
146165 mainBinding = ActivityMainBinding .inflate (getLayoutInflater ());
147166 drawerLayoutBinding = mainBinding .drawerLayout ;
@@ -176,23 +195,38 @@ protected void onCreate(final Bundle savedInstanceState) {
176195 && ReleaseVersionUtil .INSTANCE .isReleaseApk ()) {
177196 UpdateSettingsFragment .askForConsentToUpdateChecks (this );
178197 }
198+
199+ Localization .migrateAppLanguageSettingIfNecessary (getApplicationContext ());
179200 }
180201
181202 @ Override
182203 protected void onPostCreate (final Bundle savedInstanceState ) {
183204 super .onPostCreate (savedInstanceState );
184205
185206 final App app = App .getApp ();
186- final SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (app );
187207
188- if (prefs .getBoolean (app .getString (R .string .update_app_key ), false )
189- && prefs .getBoolean (app .getString (R .string .update_check_consent_key ), false )) {
208+ if (sharedPreferences .getBoolean (app .getString (R .string .update_app_key ), false )
209+ && sharedPreferences
210+ .getBoolean (app .getString (R .string .update_check_consent_key ), false )) {
190211 // Start the worker which is checking all conditions
191212 // and eventually searching for a new version.
192213 NewVersionWorker .enqueueNewVersionCheckingWork (app , false );
193214 }
194215 }
195216
217+ @ Override
218+ protected void onStart () {
219+ super .onStart ();
220+ sharedPrefEditor .putBoolean (KEY_IS_IN_BACKGROUND , false ).apply ();
221+ Log .d (TAG , "App moved to foreground" );
222+ }
223+
224+ @ Override
225+ protected void onStop () {
226+ super .onStop ();
227+ sharedPrefEditor .putBoolean (KEY_IS_IN_BACKGROUND , true ).apply ();
228+ Log .d (TAG , "App moved to background" );
229+ }
196230 private void setupDrawer () throws ExtractionException {
197231 addDrawerMenuForCurrentService ();
198232
@@ -492,21 +526,19 @@ protected void onResume() {
492526 ErrorUtil .showUiErrorSnackbar (this , "Setting up service toggle" , e );
493527 }
494528
495- final SharedPreferences sharedPreferences =
496- PreferenceManager .getDefaultSharedPreferences (this );
497529 if (sharedPreferences .getBoolean (Constants .KEY_THEME_CHANGE , false )) {
498530 if (DEBUG ) {
499531 Log .d (TAG , "Theme has changed, recreating activity..." );
500532 }
501- sharedPreferences . edit () .putBoolean (Constants .KEY_THEME_CHANGE , false ).apply ();
533+ sharedPrefEditor .putBoolean (Constants .KEY_THEME_CHANGE , false ).apply ();
502534 ActivityCompat .recreate (this );
503535 }
504536
505537 if (sharedPreferences .getBoolean (Constants .KEY_MAIN_PAGE_CHANGE , false )) {
506538 if (DEBUG ) {
507539 Log .d (TAG , "main page has changed, recreating main fragment..." );
508540 }
509- sharedPreferences . edit () .putBoolean (Constants .KEY_MAIN_PAGE_CHANGE , false ).apply ();
541+ sharedPrefEditor .putBoolean (Constants .KEY_MAIN_PAGE_CHANGE , false ).apply ();
510542 NavigationHelper .openMainActivity (this );
511543 }
512544
@@ -848,7 +880,8 @@ private void openMiniPlayerUponPlayerStarted() {
848880 @ Override
849881 public void onReceive (final Context context , final Intent intent ) {
850882 if (Objects .equals (intent .getAction (),
851- VideoDetailFragment .ACTION_PLAYER_STARTED )) {
883+ VideoDetailFragment .ACTION_PLAYER_STARTED )
884+ && PlayerHolder .getInstance ().isPlayerOpen ()) {
852885 openMiniPlayerIfMissing ();
853886 // At this point the player is added 100%, we can unregister. Other actions
854887 // are useless since the fragment will not be removed after that.
@@ -860,6 +893,10 @@ public void onReceive(final Context context, final Intent intent) {
860893 final IntentFilter intentFilter = new IntentFilter ();
861894 intentFilter .addAction (VideoDetailFragment .ACTION_PLAYER_STARTED );
862895 registerReceiver (broadcastReceiver , intentFilter );
896+
897+ // If the PlayerHolder is not bound yet, but the service is running, try to bind to it.
898+ // Once the connection is established, the ACTION_PLAYER_STARTED will be sent.
899+ PlayerHolder .getInstance ().tryBindIfNeeded (this );
863900 }
864901 }
865902
0 commit comments