Android Open Source - android-furk-app My Files Adapter






From Project

Back to project page android-furk-app.

License

The source code is released under:

Apache License

If you think the Android project android-furk-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.simple.furk.adapter;
// w w w  .j  av  a2  s . c  o m
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.ion.Ion;
import com.simple.furk.APIClient;
import com.simple.furk.APIUtils;
import com.simple.furk.Furk;
import com.simple.furk.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class MyFilesAdapter extends FilesAdapter {

    private SharedPreferences mPrefs;
    private int loaderPos;
    private Boolean firstLoad;
    private final Furk.MyFilesFragment myFilesFragment;

    public MyFilesAdapter(Furk.MyFilesFragment myFilesFragment) {
        super(myFilesFragment.getActivity());
        this.myFilesFragment = myFilesFragment;
        mPrefs = context.getSharedPreferences("furk_cache",0);
        loaderPos = 0;
        firstLoad = true;
    }

    @Override
    public void Execute(Object... args)
    {
        if(mPrefs.contains("my_files_cache"))
            try {
                jArrayChain = new JSONArrayChain(new JSONArray(mPrefs.getString("my_files_cache",null)));
            } catch (JSONException e) {
                jArrayChain.clear();
            }
        firstLoad = true;

        APIClient.get(myFilesFragment.getActivity(),"file/get",this);
        ((Furk)context).setRefreshing();
    }


    public void saveState()
    {
        if(jArrayChain.length() > 0)
        {
            SharedPreferences.Editor ed = mPrefs.edit();
            ed.putString("my_files_cache",jArrayChain.getJSONArray(0).toString());
            ed.commit();
        }
    }

    public void processAPIResponse(JSONObject response){
        String message = "No files";
            try {
                JSONArray jArray = response.getJSONArray("files");
                if (firstLoad) {
                    jArrayChain.clear();
                    firstLoad = false;
                }
                jArrayChain.addJSONArray(jArray);
            }
            catch (Exception e) {
                    jArrayChain.clear();
                    message =  "Invalid server response. "+ e.getMessage();
            }
            finally {
                myFilesFragment.setEmptyText(message);
                notifyDataSetChanged();
                ((Furk) context).doneRefrshing();
            }
        }

    @Override
    public void processAPIError(Throwable e) {

        try
        {
            myFilesFragment.setEmptyText(e.getMessage());
            ((Furk) context).doneRefrshing();
        }
        catch (IllegalStateException e1)
        {
            Log.d("furk","fragment disposed before async api request finished");
        }
        finally {
            jArrayChain.clear();
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        int length = jArrayChain.length();
        if(length > 0)
           //Add an extra listview item for the loading spinner
           return length + 1;
        else
            return 0;
    }

    @Override
    public String getItem(int i) {
        return null;
    }

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


    private void getMoreFiles()
    {
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("offset", String.valueOf(jArrayChain.length()));
        APIClient.get(myFilesFragment.getActivity(),"file/get", params,this);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View rowView;
        if (position == jArrayChain.length())
        {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item_loading, parent, false);

            //Avoid calling getMoreFiles more than once
            if(loaderPos != position)
            {
                loaderPos = position;
                getMoreFiles();
            }
        }
        else
        {
            //Recycle convertView. Check for loader list item because it can't be recycled
            if(convertView == null || convertView.getId() == R.id.listViewLoader)
            {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflater.inflate(R.layout.list_item_file, parent, false);
            }
            else
            {
                rowView = convertView;
            }

            TextView title = (TextView) rowView.findViewById(R.id.listview_title);
            TextView description = (TextView) rowView.findViewById(R.id.listview_description);

            String strTitle = "Unknown";
            String strDescription = "";
            try {
                JSONObject jsonObj = jArrayChain.getJSONObject(position);
                strTitle = Html.fromHtml(jsonObj.getString("name")).toString();
                strDescription = APIUtils.formatSize(jsonObj.getString("size"));
                strDescription += "  " + APIUtils.formatDate(jsonObj.getString("ctime"));
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
            finally {
                title.setText(strTitle);
                description.setText(strDescription);
            }
        }

        return rowView;
    }


}




Java Source Code List

com.simple.furk.APIClient.java
com.simple.furk.APIUtils.java
com.simple.furk.FileActivity.java
com.simple.furk.FurkServiceGuideExtension.java
com.simple.furk.Furk.java
com.simple.furk.LogExceptionHandler.java
com.simple.furk.LoginActivity.java
com.simple.furk.MainActivity.java
com.simple.furk.NavigationDrawerFragment.java
com.simple.furk.SearchActivity.java
com.simple.furk.SettingsActivity.java
com.simple.furk.adapter.ActiveFilesAdapter.java
com.simple.furk.adapter.FilesAdapter.java
com.simple.furk.adapter.MyFilesAdapter.java
com.simple.furk.adapter.SearchFilesAdapter.java
com.simple.furk.adapter.TFilesAdapter.java