cn.com.teamlink.workbench.ExplorerActivity.java Source code

Java tutorial

Introduction

Here is the source code for cn.com.teamlink.workbench.ExplorerActivity.java

Source

/*
 * Copyright (C) 2015 The CloudKit Open Source Project
 *
 * 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 cn.com.teamlink.workbench;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.Volley;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.com.teamlink.workbench.utils.InputStreamVolleyRequest;

/**
 * ExplorerActivity.java
 *
 * @author hongquanli <hongquanli@qq.com>
 * @version 1.0 20161227 ?11:38:34
 */
public class ExplorerActivity extends AppCompatActivity {

    private static final String TAG = "ExplorerActivity";

    private GridView gridview;
    protected ArrayList<HashMap<String, String>> gridViewListItems;
    // ?
    protected BaseAdapter gridViewListItemAdapter;

    private InputStreamVolleyRequest request;

    public void onCreate(Bundle savedInstanceState) {
        // 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // ?
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_explorer);

        try {
            gridview = (GridView) findViewById(R.id.explorer_grid_view);

            gridViewListItems = new ArrayList<HashMap<String, String>>();
            /*
            gridViewListItemAdapter = new SimpleAdapter(
                this,
                // ???
                gridViewListItems,
                R.layout.explorer_item,
                new String[]{"item_text"},
                new int[]{R.id.item_text_view}
            );
            */

            gridViewListItemAdapter = new BaseAdapter() {

                @Override
                public int getCount() {
                    return gridViewListItems.size();
                }

                @Override
                public Object getItem(int position) {
                    return null;
                }

                @Override
                public long getItemId(int position) {
                    return 0;
                }

                @Override
                public int getItemViewType(int position) {
                    // TODO Auto-generated method stub
                    Map<String, String> item = gridViewListItems.get(position);
                    if (item.get("type").equals("GRID_HEADER")) {
                        return 1;
                    }
                    return 0;
                }

                @Override
                public int getViewTypeCount() {
                    return 2;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    if (getItemViewType(position) == 0) {
                        GridViewHeaderHolder gridViewHeaderHolder = null;

                        if (convertView != null) {
                            gridViewHeaderHolder = (GridViewHeaderHolder) convertView.getTag();
                        } else {
                            gridViewHeaderHolder = new GridViewHeaderHolder();

                            LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
                            convertView = inflater.inflate(R.layout.explorer_item, parent, false);

                            gridViewHeaderHolder.itemTextView = (TextView) convertView
                                    .findViewById(R.id.item_text_view);
                            convertView.setTag(gridViewHeaderHolder);
                        }

                        gridViewHeaderHolder.itemTextView
                                .setText(gridViewListItems.get(position).get("item_text").toString());

                    } else {

                        GridViewItemHolder gridViewItemHolder = null;

                        if (convertView != null) {
                            gridViewItemHolder = (GridViewItemHolder) convertView.getTag();
                        } else {
                            gridViewItemHolder = new GridViewItemHolder();

                            LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
                            convertView = inflater.inflate(R.layout.explorer_item, parent, false);
                            convertView.setBackgroundResource(R.color.deepGray);

                            gridViewItemHolder.itemTextView = (TextView) convertView
                                    .findViewById(R.id.item_text_view);
                            convertView.setTag(gridViewItemHolder);
                        }

                        gridViewItemHolder.itemTextView
                                .setText(gridViewListItems.get(position).get("item_text").toString());

                    }

                    return convertView;
                }
            };

            // 
            gridview.setAdapter(gridViewListItemAdapter);
            // ??
            gridview.setOnItemClickListener(new ItemClickListener());

            // 
            addHeader();

            // ?
            addData();
        } catch (Exception e) {
            Dialog alertDialog = new AlertDialog.Builder(this).setTitle("??").setMessage(e.getMessage())
                    .setIcon(R.mipmap.ic_launcher).create();
            alertDialog.show();
        }

        try {
            // ??
            String fileName = "fileDemo.txt";
            // ??
            FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
            String text = "Some data";
            // ?
            fos.write(text.getBytes());
            // ?
            fos.flush();
            // FileOutputStream
            fos.close();

            // String fileName = "fileDemo.txt";
            FileInputStream fis = openFileInput(fileName);
            byte[] readBytes = new byte[fis.available()];
            while (fis.read(readBytes) != -1) {

            }

            Dialog alertDialog = new AlertDialog.Builder(this).setTitle("??").setMessage(new String(readBytes))
                    .setIcon(R.mipmap.ic_launcher).create();
            alertDialog.show();
        } catch (Exception e) {

            Dialog alertDialog = new AlertDialog.Builder(this).setTitle("??")
                    .setMessage(e.getLocalizedMessage()).setIcon(R.mipmap.ic_launcher).create();
            alertDialog.show();

            /*
            Dialog alertDialog = new AlertDialog.Builder(this).
                setTitle("").
                setMessage("???").
                setIcon(R.mipmap.ic_launcher).
                setPositiveButton(
                        "",
                        new DialogInterface.OnClickListener() {
                
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        }
                ).
                setNegativeButton("?", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).
                setNeutralButton("", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).
                create();
            alertDialog.show();
            */

            /*
            final String[] arrayFruit = new String[]{"", "?", "?", ""};
            Dialog alertDialog = new AlertDialog
                .Builder(this).
                setTitle("??").
                setIcon(R.mipmap.ic_launcher)
                .setItems(
                        arrayFruit,
                        new DialogInterface.OnClickListener() {
                
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(ExplorerActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
                            }
                        }
                )
                .setNegativeButton(
                        "?",
                        new DialogInterface.OnClickListener() {
                
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        }
                ).create();
            alertDialog.show();
            */

            /*
            // int selectedFruitIndex = 0;
            final String[] arrayFruit = new String[]{"", "?", "?", ""};
                
            Dialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("??")
                .setIcon(R.mipmap.ic_launcher)
                .setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selectedFruitIndex = which;
                    }
                })
                .setPositiveButton("", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("?", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).create();
            alertDialog.show();
            */

            /*
            final String[] arrayFruit = new String[]{"", "?", "?", ""};
            final boolean[] arrayFruitSelected = new boolean[]{true, true, false, false};
                
            Dialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("??")
                .setIcon(R.mipmap.ic_launcher)
                .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        arrayFruitSelected[which] = isChecked;
                    }
                })
                .setPositiveButton("", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (int i = 0; i < arrayFruitSelected.length; i++) {
                            if (arrayFruitSelected[i] == true) {
                                stringBuilder.append(arrayFruit[i] + "?");
                            }
                        }
                        Toast.makeText(ExplorerActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("?", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).create();
            alertDialog.show();
            */

            /*
            // ?View
            LayoutInflater layoutInflater = LayoutInflater.from(this);
            View myLoginView = layoutInflater.inflate(R.layout.login, null);
                
            Dialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("")
                .setIcon(R.mipmap.ic_launcher)
                .setView(myLoginView)
                .setPositiveButton("", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                })
                .setNegativeButton("?", new DialogInterface.OnClickListener() {
                
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).create();
            alertDialog.show();
            */
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // TODO ORIENTATION_LANDSCAPE
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            // TODO ORIENTATION_PORTRAIT
        }
    }

    private class ItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            try {
                /*
                HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);
                    
                // ItemItemText
                Toast.makeText(getApplicationContext(), (String) item.get("item_text"), Toast.LENGTH_SHORT).show();
                */

                // Verify that all required contact permissions have been granted.
                if (ContextCompat.checkSelfPermission(getApplicationContext(),
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                    // WRITE_EXTERNAL_STORAGE permissions have not been granted.
                    /*
                    Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions.");
                    // WRITE_EXTERNAL_STORAGE??
                    ActivityCompat.requestPermissions(ExplorerActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                    */

                    if (ActivityCompat.shouldShowRequestPermissionRationale(ExplorerActivity.this,
                            Manifest.permission.READ_CONTACTS)
                            || ActivityCompat.shouldShowRequestPermissionRationale(ExplorerActivity.this,
                                    Manifest.permission.WRITE_CONTACTS)) {

                        // Provide an additional rationale to the user if the permission was not granted
                        // and the user would benefit from additional context for the use of the permission.
                        // For example, if the request has been denied previously.
                        Log.i(TAG, "Displaying contacts permission rationale to provide additional context.");

                        // Display a SnackBar with an explanation and a button to trigger the request.
                        Snackbar.make(view, "??:", Snackbar.LENGTH_INDEFINITE)
                                .setAction("", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        ActivityCompat.requestPermissions(ExplorerActivity.this,
                                                new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
                                    }
                                }).show();
                    } else {
                        // Contact permissions have not been granted yet. Request them directly.
                        ActivityCompat.requestPermissions(ExplorerActivity.this,
                                new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
                    }
                    // END_INCLUDE(contacts_permission_request)
                } else {

                    // Contact permissions have been granted. Show the contacts fragment.
                    Log.i(TAG,
                            "WRITE_EXTERNAL_STORAGE permissions have already been granted. Displaying contact details.");

                    // TODO
                    request = new InputStreamVolleyRequest(Request.Method.GET,
                            "http://www.guanmaoyun.com/main.html", new Response.Listener<byte[]>() {
                                @Override
                                public void onResponse(byte[] bytes) {
                                    // HashMap<String, Object> map = new HashMap<String, Object>();
                                    try {
                                        if (bytes != null) {

                                            /*
                                            // Read file name from headers (We have configured API to send file name in "Content-Disposition" header in following format: "File-Name:File-Format" example "MyDoc:pdf"
                                            String content = request.responseHeaders.get("Content-Disposition");
                                            StringTokenizer st = new StringTokenizer(content, "=");
                                                
                                            int numberOfTokens = st.countTokens();
                                            String[] arrTag = new String[numberOfTokens];
                                            int x = 0;
                                            while (st.hasMoreTokens()) {
                                            arrTag[x] = st.nextToken();
                                            }
                                                
                                            String filename = arrTag[1];
                                            */

                                            String filename = "test.html";
                                            // filename = filename.replace(":", ".");
                                            Log.i("DEBUG::FILE NAME", filename);

                                            InputStream input = null;
                                            BufferedOutputStream output = null;
                                            try {
                                                long lenghtOfFile = bytes.length;

                                                //covert reponse to input stream
                                                input = new ByteArrayInputStream(bytes);

                                                //Create a file on desired path and write stream data to it
                                                // File path = Environment.getExternalStorageDirectory();
                                                // File sd = Environment.getExternalStorageDirectory();
                                                // sd.canWrite();

                                                Log.d(TAG, "sdcard???");
                                                //sdcard??
                                                if (!Environment.getExternalStorageState()
                                                        .equals(Environment.MEDIA_MOUNTED)) {
                                                    Log.d(TAG, "sdcard??!");
                                                    Toast.makeText(getApplicationContext(), "SDCard!",
                                                            Toast.LENGTH_LONG);
                                                    return;
                                                }
                                                ;

                                                // File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                                                // File path = Environment.getDownloadCacheDirectory();
                                                // File path = Environment.getDataDirectory();
                                                // Log.i("DEBUG::DataDirectory:", Environment.getDataDirectory().getAbsolutePath());
                                                File path = Environment.getExternalStorageDirectory();

                                                // ?sdcard?
                                                long blockSize = 0;
                                                long blockCount = 0;
                                                StatFs statFs = new StatFs(
                                                        Environment.getExternalStorageDirectory().getPath());
                                                // FIXME Make sure we're running on HONEYCOMB or higher to use APIs
                                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                                                    blockSize = statFs.getBlockSizeLong();
                                                    blockCount = statFs.getBlockSizeLong();
                                                } else {
                                                    blockSize = statFs.getBlockSize();
                                                    blockCount = statFs.getBlockCount();
                                                }
                                                long sdCardSize = blockSize * blockCount;
                                                Log.d(TAG, String.valueOf(sdCardSize));

                                                // new java.io.File((getActivity().getApplicationContext().getFileStreamPath("FileName.xml").getPath()));
                                                // org.apache.commons.io.FileUtils.copyInputStreamToFile(is, file);
                                                Log.i("DEBUG::FILE PATH", path.getAbsolutePath());

                                                File file = new File(path, filename);
                                                // map.put("resume_path", file.toString());
                                                Log.i("DEBUG::FILE PATH", file.getAbsolutePath());

                                                output = new BufferedOutputStream(new FileOutputStream(file));
                                                byte data[] = new byte[1024];

                                                int count = 0;
                                                long total = 0;

                                                while ((count = input.read(data)) != -1) {
                                                    total += count;
                                                    output.write(data, 0, count);
                                                }
                                            } catch (IOException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                            } finally {
                                                if (output != null) {
                                                    output.flush();
                                                }
                                                if (output != null) {
                                                    output.close();
                                                }
                                                if (output != null) {
                                                    input.close();
                                                }
                                            }
                                        }
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                                        Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),
                                                Toast.LENGTH_LONG);
                                    }
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError volleyError) {

                                }
                            }, null);
                    RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack());
                    mRequestQueue.add(request);
                }

            } catch (Exception e) {

                Snackbar.make(view, e.getLocalizedMessage() + e.getMessage(), Snackbar.LENGTH_SHORT).show();
                /*
                Dialog alertDialog = new AlertDialog.Builder(getApplicationContext()).
                    setTitle("??").
                    setMessage(e.getLocalizedMessage()).
                    setIcon(R.mipmap.ic_launcher).
                    create();
                alertDialog.show();
                */
            }
        }
    }

    /*
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    handleGrantResults(requestCode,grantResults);
    }
        
    private void handleGrantResults(int requestCode, int[] grantResults) {
    if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission Granted ???xxx
        } else {
            // Permission Denied ??xx?
        }
    }
    }
        
    private void handleContactPermission() {
    if (Integer.parseInt(Build.VERSION.SDK)>=23) {
        int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_CONTACTS);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) {
                showMessageOKCancel("You need to allow access to Contacts",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestContactPermission();//???
                            }
                        }
                );
                return;
            }
            requestContactPermission();//???
        }
    }
    }
        
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      List<Fragment> fragments = getChildFragmentManager().getFragments();
      if (fragments != null) {
      for (Fragment fragment : fragments) {
          if (fragment != null) {
              fragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
          }
      }
      }
    }
    */

    public void addHeader() {
        String items[] = { "", "", "", "", "", "" };
        for (String item : items) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("item_text", item);
            map.put("type", "GRID_HEADER");
            gridViewListItems.add(map);
        }
        gridViewListItemAdapter.notifyDataSetChanged();
    }

    public void addData() {

        for (int i = 0; i < 3; i++) {
            String items[] = { "", "~~~~~~~~~~~~", "", "", "",
                    "~~~~~~~~~~~~" };
            for (String item : items) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("item_text", item);
                map.put("type", "GRID_ITEM");
                gridViewListItems.add(map);
            }
        }
        // ?
        gridViewListItemAdapter.notifyDataSetChanged();
    }

    // 
    public void removeAll() {
        gridViewListItems.clear();
        gridViewListItemAdapter.notifyDataSetChanged();
    }

    static class GridViewHeaderHolder {
        TextView itemTextView;
    }

    static class GridViewItemHolder {
        TextView itemTextView;
    }
}