/*
 * Copyright (C)  Justson(https://github.com/Justson/AgentWeb)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.just.agentweb;

import android.app.Activity;
import android.webkit.DownloadListener;
import android.webkit.WebView;


/**
 * @author cenxiaozhong
 * @since 1.0.0
 */
public class AgentWebSettingsImpl extends AbsAgentWebSettings {
	private AgentWeb mAgentWeb;

	@Override
	protected void bindAgentWebSupport(AgentWeb agentWeb) {
		this.mAgentWeb = agentWeb;
	}

	@Override
	public WebListenerManager setDownloader(WebView webView, DownloadListener downloadListener) {
		return super.setDownloader(webView, downloadListener == null ?
				DefaultDownloadImpl.create((Activity) webView.getContext(), webView, mAgentWeb.getPermissionInterceptor()) :
				downloadListener);
	}
}

@@ -680,16 +680,87 @@
 tivity;%0A
+import android.content.Context;%0Aimport android.content.ContextWrapper;%0A
 import a
@@ -936,17 +936,20 @@
 tings %7B%0A
-%09
+    
 private 
@@ -969,17 +969,20 @@
 ntWeb;%0A%0A
-%09
+    
 @Overrid
@@ -983,17 +983,20 @@
 verride%0A
-%09
+    
 protecte
@@ -1043,18 +1043,24 @@
 tWeb) %7B%0A
-%09%09
+        
 this.mAg
@@ -1082,13 +1082,19 @@
 eb;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 @Ove
@@ -1099,17 +1099,20 @@
 verride%0A
-%09
+    
 public W
@@ -1201,45 +1201,266 @@
 ) %7B%0A
-%09%09return super.setDownloader(webView,
+        // Fix Android 5.1 crashing:%0A        // ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity%0A        if (downloadListener == null) %7B%0A            Activity activity = getActivityByContext(webView.getContext());%0A           
  dow
@@ -1478,21 +1478,9 @@
 er =
-= null ?%0A%09%09%09%09
+ 
 Defa
@@ -1506,26 +1506,25 @@
 ate(
-(A
+a
 ctivity
-)
+,
  webView
 .get
@@ -1523,96 +1523,607 @@
 View
-.getContext(), webView, mAgentWeb.getPermissionInterceptor()) :%0A%09%09%09%09downloadListener);%0A%09%7D
+, mAgentWeb.getPermissionInterceptor());%0A        %7D%0A        return super.setDownloader(webView, downloadListener);%0A    %7D%0A%0A    /**%0A     * Copy from com.blankj.utilcode.util.ActivityUtils#getActivityByView%0A     */%0A    private Activity getActivityByContext(Context context) %7B%0A        if (context instanceof Activity) return (Activity) context;%0A        while (context instanceof ContextWrapper) %7B%0A            if (context instanceof Activity) %7B%0A                return (Activity) context;%0A            %7D%0A            context = ((ContextWrapper) context).getBaseContext();%0A        %7D%0A        return null;%0A    %7D%0A
 %0A%7D%0A

Fix Android 5.1 crashing: android.app.ContextImpl cannot be cast to android.app.Activity