/*
 * Copyright 2016 Hippo Seven
 *
 * 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.hippo.ehviewer.ui;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hippo.ehviewer.R;
import com.hippo.rippleold.RippleSalon;
import com.hippo.widget.DirExplorer;

import java.io.File;

public class DirPickerActivity extends ToolbarActivity
        implements View.OnClickListener, DirExplorer.OnChangeDirListener {

    public static final String KEY_FILE_URI = "file_uri";

    private TextView mPath;
    private DirExplorer mDirExplorer;
    private View mOk;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dir_picker);
        setNavigationIcon(R.drawable.v_arrow_left_dark_x24);

        mPath = (TextView) findViewById(R.id.path);
        mDirExplorer = (DirExplorer) findViewById(R.id.dir_explorer);
        mOk = findViewById(R.id.ok);

        File file = null;
        Intent intent = getIntent();
        if (intent != null) {
            Uri fileUri = intent.getParcelableExtra(KEY_FILE_URI);
            if (fileUri != null) {
                file = new File(fileUri.getPath());
            }
        }

        mDirExplorer.setCurrentFile(file);
        mDirExplorer.setOnChangeDirListener(this);

        RippleSalon.addRipple(mOk, false);

        mOk.setOnClickListener(this);

        mPath.setText(mDirExplorer.getCurrentFile().getPath());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOk == v) {
            File file = mDirExplorer.getCurrentFile();
            if (!file.canWrite()) {
                Toast.makeText(this, R.string.directory_not_writable, Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent();
                intent.setData(Uri.fromFile(file));
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    }

    @Override
    public void onChangeDir(File dir) {
        mPath.setText(dir.getPath());
    }
}

@@ -807,16 +807,60 @@
 onNull;%0A
+import android.support.annotation.Nullable;%0A
 import a
@@ -881,16 +881,16 @@
 nuItem;%0A
-
 import a
@@ -1074,16 +1074,53 @@
 xplorer;
+%0Aimport com.hippo.yorozuya.ViewUtils;
 %0A%0Aimport
@@ -1325,16 +1325,97 @@
 _uri%22;%0A%0A
+    /*---------------%0A     Whole life cycle%0A     ---------------*/%0A    @Nullable%0A
     priv
@@ -1434,16 +1434,30 @@
  mPath;%0A
+    @Nullable%0A
     priv
@@ -1486,16 +1486,30 @@
 plorer;%0A
+    @Nullable%0A
     priv
@@ -1829,29 +1829,35 @@
 xtView) 
-findViewById(
+ViewUtils.$$(this, 
 R.id.pat
@@ -1897,29 +1897,35 @@
 plorer) 
-findViewById(
+ViewUtils.$$(this, 
 R.id.dir
@@ -1954,21 +1954,27 @@
 k = 
-findViewById(
+ViewUtils.$$(this, 
 R.id
@@ -2493,32 +2493,185 @@
 Path());%0A    %7D%0A%0A
+    @Override%0A    protected void onDestroy() %7B%0A        super.onDestroy();%0A%0A        mPath = null;%0A        mDirExplorer = null;%0A        mOk = null;%0A    %7D%0A%0A
     @Override%0A  
@@ -3026,24 +3026,102 @@
 mOk == v) %7B%0A
+            if (null == mDirExplorer) %7B%0A                return;%0A            %7D%0A
             
@@ -3569,24 +3569,57 @@
 File dir) %7B%0A
+        if (null != mPath) %7B%0A    
         mPat
@@ -3640,20 +3640,30 @@
 getPath());%0A
+        %7D%0A
     %7D%0A%7D%0A

Make DirPickerActivity safe