package org.ruby_china.rubychina;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.basecamp.turbolinks.TurbolinksAdapter;
import com.basecamp.turbolinks.TurbolinksSession;
import com.basecamp.turbolinks.TurbolinksView;

public class BaseActivity extends AppCompatActivity implements TurbolinksAdapter {
    protected static final String INTENT_URL = "intentUrl";

    protected String location;
    protected TurbolinksView turbolinksView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        location = getIntent().getStringExtra(INTENT_URL);
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        TurbolinksSession.getDefault(this)
                .activity(this)
                .adapter(this)
                .restoreWithCachedSnapshot(true)
                .view(turbolinksView)
                .visit(location);
    }

    @Override
    public void onPageFinished() {

    }

    @Override
    public void onReceivedError(int errorCode) {

    }

    @Override
    public void pageInvalidated() {

    }

    @Override
    public void requestFailedWithStatusCode(int statusCode) {
        switch (statusCode) {
            case 401:
                visitProposedToLocationWithAction(getString(R.string.root_url) + "/account/sign_in", "advance");
                break;
            default:
                break;
        }
    }

    @Override
    public void visitCompleted() {

    }

    @Override
    public void visitProposedToLocationWithAction(String location, String action) {
        Intent intent;

        Uri uri = Uri.parse(location);

        if (location.startsWith(getString(R.string.root_url))) {
            String path = uri.getPath();
            if (path.matches("/topics/\\d+")) {
                intent = new Intent(this, TopicActivity.class);
                intent.putExtra(INTENT_URL, location);
            } else if (path.matches("/topics/new")) {
                intent = new Intent(this, TopicNewActivity.class);
                intent.putExtra(INTENT_URL, location);
            } else if (path.matches("/topics/\\d+/edit")) {
                intent = new Intent(this, TopicEditActivity.class);
                intent.putExtra(INTENT_URL, location);
            } else if (path.matches("/topics/\\d+/replies/\\d+/edit")) {
                intent = new Intent(this, ReplyEditActivity.class);
                intent.putExtra(INTENT_URL, location);
            } else if (path.matches("/account/edit")) {
                intent = new Intent(this, SettingsActivity.class);
                intent.putExtra(INTENT_URL, location);
            } else {
                intent = new Intent(this, EmptyActivity.class);
                intent.putExtra(INTENT_URL, location);
            }
        } else {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
        }

        this.startActivity(intent);
    }

}

@@ -136,76 +136,100 @@
 ort.
-annotation.Nullable;%0Aimport android.support.v7.app.AppCompatActivity
+v7.app.AppCompatActivity;%0Aimport android.webkit.ValueCallback;%0Aimport android.webkit.WebView
 ;%0A%0Ai
@@ -604,126 +604,1368 @@
     
-@Override%0A    protected void onCreate(@Nullable Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState
+private ValueCallback%3CUri%5B%5D%3E mFilePathCallback;%0A    private final int REQUEST_SELECT_FILE = 1001;%0A    private boolean onSelectFileCallback = false;%0A%0A    class WebChromeClient extends android.webkit.WebChromeClient %7B%0A        @Override%0A        public boolean onShowFileChooser(WebView webView, ValueCallback%3CUri%5B%5D%3E filePathCallback, FileChooserParams fileChooserParams) %7B%0A            mFilePathCallback = filePathCallback;%0A            startActivityForResult(fileChooserParams.createIntent(), REQUEST_SELECT_FILE);%0A%0A            return true;%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        switch (requestCode) %7B%0A            case REQUEST_SELECT_FILE:%0A                if (resultCode == RESULT_OK && data != null) %7B%0A                    mFilePathCallback.onReceiveValue(new Uri%5B%5D %7B data.getData() %7D);%0A                %7D else %7B%0A                    mFilePathCallback.onReceiveValue(null);%0A                %7D%0A                onSelectFileCallback = true;%0A                break;%0A            default:%0A                super.onActivityResult(requestCode, resultCode, data);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A%0A        TurbolinksSession.getDefault(this).getWebView().setWebChromeClient(new WebChromeClient()
 );%0A%0A
@@ -2101,24 +2101,65 @@
 Restart();%0A%0A
+        if (!onSelectFileCallback) %7B%0A    
         Turb
@@ -2205,16 +2205,20 @@
         
+   
+ 
 .activit
@@ -2241,16 +2241,20 @@
         
+    
 .adapter
@@ -2276,16 +2276,20 @@
         
+ 
+   
 .restore
@@ -2329,16 +2329,20 @@
         
+  
+  
 .view(tu
@@ -2371,16 +2371,20 @@
         
+    
 .visit(l
@@ -2389,24 +2389,93 @@
 (location);%0A
+        %7D else %7B%0A            onSelectFileCallback = false;%0A        %7D%0A
     %7D%0A%0A    @

Fix WebView file select