浏览代码

Merge pull request #2225 from nextcloud/windowNullable

getWindow(): The current window, or null if the activity is not visual.
Andy Scherzinger 6 年之前
父节点
当前提交
0d4fa10676

+ 8 - 4
src/main/java/com/owncloud/android/authentication/PassCodeManager.java

@@ -24,6 +24,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.os.Build;
 import android.os.PowerManager;
+import android.view.Window;
 import android.view.WindowManager;
 
 import com.owncloud.android.MainApp;
@@ -67,10 +68,13 @@ public class PassCodeManager {
     private PassCodeManager() {}
 
     public void onActivityCreated(Activity activity) {
-        if (passCodeIsEnabled() || deviceCredentialsAreEnabled(activity)) {
-            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
-        } else {
-            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
+        Window window = activity.getWindow();
+        if (window != null) {
+            if (passCodeIsEnabled() || deviceCredentialsAreEnabled(activity)) {
+                window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
+            } else {
+                window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
+            }
         }
     }
 

+ 4 - 1
src/main/java/com/owncloud/android/ui/activity/ExternalSiteWebView.java

@@ -69,7 +69,10 @@ public class ExternalSiteWebView extends FileActivity {
         showSidebar = extras.getBoolean(EXTRA_SHOW_SIDEBAR);
 
         // show progress
-        getWindow().requestFeature(Window.FEATURE_PROGRESS);
+        Window window = getWindow();
+        if (window != null) {
+            window.requestFeature(Window.FEATURE_PROGRESS);
+        }
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.externalsite_webview);

+ 7 - 2
src/main/java/com/owncloud/android/ui/activity/PassCodeActivity.java

@@ -34,6 +34,7 @@ import android.text.TextWatcher;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.view.Window;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.Button;
 import android.widget.EditText;
@@ -99,8 +100,7 @@ public class PassCodeActivity extends AppCompatActivity {
         mPassCodeEditTexts[0].setTextColor(elementColor);
         mPassCodeEditTexts[0].getBackground().setColorFilter(elementColor, PorterDuff.Mode.SRC_ATOP);
         mPassCodeEditTexts[0].requestFocus();
-        getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
-
+        
         mPassCodeEditTexts[1] = findViewById(R.id.txt1);
         mPassCodeEditTexts[1].setTextColor(elementColor);
         mPassCodeEditTexts[1].getBackground().setColorFilter(elementColor, PorterDuff.Mode.SRC_ATOP);
@@ -113,6 +113,11 @@ public class PassCodeActivity extends AppCompatActivity {
         mPassCodeEditTexts[3].setTextColor(elementColor);
         mPassCodeEditTexts[3].getBackground().setColorFilter(elementColor, PorterDuff.Mode.SRC_ATOP);
 
+        Window window = getWindow();
+        if (window != null) {
+            window.setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        }
+        
         if (ACTION_CHECK.equals(getIntent().getAction())) {
             /// this is a pass code request; the user has to input the right value
             mPassCodeHdr.setText(R.string.pass_code_enter_pass_code);

+ 21 - 16
src/main/java/com/owncloud/android/ui/activity/Preferences.java

@@ -55,6 +55,7 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.Window;
 import android.webkit.URLUtil;
 
 import com.owncloud.android.BuildConfig;
@@ -767,25 +768,29 @@ public class Preferences extends PreferenceActivity
 
     private void setupActionBar() {
         ActionBar actionBar = getDelegate().getSupportActionBar();
-        actionBar.setDisplayHomeAsUpEnabled(true);
-        ThemeUtils.setColoredTitle(actionBar, getString(R.string.actionbar_settings), this);
-        actionBar.setBackgroundDrawable(new ColorDrawable(ThemeUtils.primaryColor(this)));
-        getWindow().getDecorView().setBackgroundDrawable(new ColorDrawable(ResourcesCompat
-                .getColor(getResources(), R.color.background_color, null)));
-
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            getWindow().setStatusBarColor(ThemeUtils.primaryDarkColor(this));
+
+        if (actionBar != null) {
+            actionBar.setDisplayHomeAsUpEnabled(true);
+            ThemeUtils.setColoredTitle(actionBar, getString(R.string.actionbar_settings), this);
+            actionBar.setBackgroundDrawable(new ColorDrawable(ThemeUtils.primaryColor(this)));
+
+            Drawable backArrow = getResources().getDrawable(R.drawable.ic_arrow_back);
+            actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontColor(this)));
         }
 
-        Drawable backArrow = getResources().getDrawable(R.drawable.ic_arrow_back);
-        actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontColor(this)));
+        Window window = getWindow();
+        if (window != null) {
+            window.getDecorView().setBackgroundDrawable(new ColorDrawable(ResourcesCompat
+                    .getColor(getResources(), R.color.background_color, null)));
+
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+                window.setStatusBarColor(ThemeUtils.primaryDarkColor(this));
+            }
 
-        // For adding content description tag to a title field in the action bar
-        int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
-        View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
-        if (actionBarTitleView != null) {    // it's null in Android 2.x
-            getWindow().getDecorView().findViewById(actionBarTitleId).
-                    setContentDescription(getString(R.string.actionbar_settings));
+            // For adding content description tag to a title field in the action bar
+            int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
+            window.getDecorView().findViewById(actionBarTitleId).
+                        setContentDescription(getString(R.string.actionbar_settings));
         }
     }
 

+ 7 - 1
src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.java

@@ -29,6 +29,7 @@ import android.support.v4.app.DialogFragment;
 import android.support.v7.app.AlertDialog;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.Window;
 import android.view.WindowManager.LayoutParams;
 import android.widget.EditText;
 import android.widget.TextView;
@@ -105,7 +106,12 @@ public class CreateFolderDialogFragment
                 .setTitle(ThemeUtils.getColoredTitle(getResources().getString(R.string.uploader_info_dirname),
                         accentColor));
         Dialog d = builder.create();
-        d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+
+        Window window = d.getWindow();
+        if (window != null) {
+            window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        }
+        
         return d;
     }    
     

+ 7 - 1
src/main/java/com/owncloud/android/ui/dialog/CredentialsDialogFragment.java

@@ -26,6 +26,7 @@ import android.support.v4.app.DialogFragment;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AlertDialog.Builder;
 import android.text.InputType;
+import android.view.Window;
 import android.view.WindowManager.LayoutParams;
 import android.webkit.HttpAuthHandler;
 import android.webkit.WebView;
@@ -104,7 +105,12 @@ public class CredentialsDialogFragment extends DialogFragment
                 .setNegativeButton(R.string.common_cancel, this);
 
         Dialog d = authDialog.create();
-        d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+
+        Window window = d.getWindow();
+        if (window != null) {
+            window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        }
+        
         return d;
     }
 

+ 7 - 1
src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java

@@ -35,6 +35,7 @@ import android.support.v4.app.DialogFragment;
 import android.support.v7.app.AlertDialog;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.Window;
 import android.view.WindowManager.LayoutParams;
 import android.widget.EditText;
 import android.widget.TextView;
@@ -119,7 +120,12 @@ public class RenameFileDialogFragment
                 .setTitle(ThemeUtils.getColoredTitle(getResources().getString(R.string.rename_dialog_title),
                         accentColor));
         Dialog d = builder.create();
-        d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+
+        Window window = d.getWindow();
+        if (window != null) {
+            window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        }
+        
         return d;
     }    
 

+ 7 - 1
src/main/java/com/owncloud/android/ui/dialog/SharePasswordDialogFragment.java

@@ -32,6 +32,7 @@ import android.support.v7.app.AlertDialog;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.Window;
 import android.view.WindowManager;
 import android.widget.EditText;
 import android.widget.TextView;
@@ -118,7 +119,12 @@ public class SharePasswordDialogFragment extends DialogFragment implements Dialo
                 .setNeutralButton(R.string.common_delete, this)
                 .setTitle(R.string.share_link_password_title);
         Dialog d = builder.create();
-        d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+
+        Window window = d.getWindow();
+        if (window != null) {
+            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        }
+        
         return d;
     }
 

+ 5 - 3
src/main/java/com/owncloud/android/utils/ThemeUtils.java

@@ -46,6 +46,7 @@ import android.support.v7.widget.AppCompatCheckBox;
 import android.support.v7.widget.SwitchCompat;
 import android.text.Html;
 import android.text.Spanned;
+import android.view.Window;
 import android.widget.ImageButton;
 import android.widget.ProgressBar;
 import android.widget.SeekBar;
@@ -314,8 +315,9 @@ public class ThemeUtils {
      * @param color            the color
      */
     public static void colorStatusBar(FragmentActivity fragmentActivity, @ColorInt int color) {
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            fragmentActivity.getWindow().setStatusBarColor(color);
+        Window window = fragmentActivity.getWindow();
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && window != null) {
+            window.setStatusBarColor(color);
         }
     }
 
@@ -414,4 +416,4 @@ public class ThemeUtils {
             return new OCCapability();
         }
     }
-}
+}