|
| 1 | +package org.opendroidphp.app.ui; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.text.format.Formatter; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.AdapterView; |
| 9 | +import android.widget.ListView; |
| 10 | + |
| 11 | +import com.actionbarsherlock.app.SherlockFragment; |
| 12 | +import com.android.volley.Response; |
| 13 | +import com.android.volley.VolleyError; |
| 14 | +import com.android.volley.toolbox.JsonArrayRequest; |
| 15 | + |
| 16 | +import org.json.JSONArray; |
| 17 | +import org.json.JSONException; |
| 18 | +import org.json.JSONObject; |
| 19 | +import org.opendroidphp.R; |
| 20 | +import org.opendroidphp.app.AppController; |
| 21 | +import org.opendroidphp.app.Constants; |
| 22 | +import org.opendroidphp.app.adapter.RepositoryListAdapter; |
| 23 | +import org.opendroidphp.app.fragments.dialogs.ConfirmDialogFragment; |
| 24 | +import org.opendroidphp.app.model.Repository; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | + |
| 28 | + |
| 29 | +public class PackageFragment extends SherlockFragment implements AdapterView.OnItemClickListener { |
| 30 | + |
| 31 | + private ListView mListView; |
| 32 | + private ArrayList<Repository> mList = new ArrayList<Repository>(); |
| 33 | + private RepositoryListAdapter mAdapter; |
| 34 | + |
| 35 | + @Override |
| 36 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 37 | + super.onCreateView(inflater, container, savedInstanceState); |
| 38 | + View rootView = inflater.inflate(R.layout.fragment_repository, container, false); |
| 39 | + prepareView(rootView); |
| 40 | + requestJsonRepository(); |
| 41 | + return rootView; |
| 42 | + } |
| 43 | + |
| 44 | + protected void requestJsonRepository() { |
| 45 | + JsonArrayRequest repositoryRequest = new JsonArrayRequest(Constants.REPOSITORY_URL, |
| 46 | + new RepositoryResponse(), |
| 47 | + new RepositoryErrorListener()); |
| 48 | + // Adding request to request queue |
| 49 | + AppController.getInstance().addToRequestQueue(repositoryRequest); |
| 50 | + } |
| 51 | + |
| 52 | + protected void prepareView(View view) { |
| 53 | + mListView = (ListView) view.findViewById(R.id.extension_list); |
| 54 | + mAdapter = new RepositoryListAdapter(getSherlockActivity(), mList); |
| 55 | + mListView.setAdapter(mAdapter); |
| 56 | + mListView.setOnItemClickListener(this); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { |
| 61 | + Repository repo = mList.get(i); |
| 62 | + String fileSize = Formatter.formatFileSize(getSherlockActivity(), repo.getSize()); |
| 63 | + String repoDetail = String.format(getString(R.string.package_detail), repo.getName(), |
| 64 | + fileSize, repo.getCopyPath()); |
| 65 | + ConfirmDialogFragment fragment = ConfirmDialogFragment. |
| 66 | + create(getSherlockActivity(), getString(R.string.package_info), repoDetail); |
| 67 | + fragment.show(getFragmentManager(), getClass().getSimpleName()); |
| 68 | + } |
| 69 | + |
| 70 | + private class RepositoryResponse implements Response.Listener<JSONArray> { |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onResponse(JSONArray jsonArray) { |
| 74 | + Repository repository; |
| 75 | + mList.clear(); |
| 76 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 77 | + try { |
| 78 | + JSONObject repoJso = jsonArray.getJSONObject(i); |
| 79 | + repository = new Repository( |
| 80 | + repoJso.getString("repo_name"), |
| 81 | + repoJso.getString("repo_url"), |
| 82 | + repoJso.getString("repo_install_location"), |
| 83 | + repoJso.getLong("repo_file_size") |
| 84 | + ); |
| 85 | + mList.add(repository); |
| 86 | + } catch (JSONException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + } |
| 89 | + } |
| 90 | + mAdapter.notifyDataSetChanged(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private class RepositoryErrorListener implements Response.ErrorListener { |
| 95 | + @Override |
| 96 | + public void onErrorResponse(VolleyError volleyError) { |
| 97 | + AppController.toast(getSherlockActivity(), "Unable to parse json file: " + volleyError.getMessage()); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments