Java tutorial
/** * Copyright (C) 2016 Zakoota@github.com * <p> * Licensed under the * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 * International License Version 2.0 (the "License") * <p> * You are free to: * <p> * 1: Share copy and redistribute the material in any medium or format * 2: Adapt remix, transform, and build upon the material * <p> * The licensor cannot revoke these freedoms as long as you follow the license terms. * <p> * 1: Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. * 2: NonCommercial You may not use the material for commercial purposes. * 3: ShareAlike If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. * 4: No additional restrictions You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. * <p> * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * https://creativecommons.org/licenses/by-nc-sa/4.0/ */ package com.raza.betternts.activities; import android.app.AlertDialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.widget.SwipeRefreshLayout; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.raza.betternts.R; import com.raza.betternts.contentprovider.NTSPostsContract; import com.raza.betternts.database.DbSchema; import com.raza.betternts.declarations.CustomCursorAdapter; import com.raza.betternts.parser.Parser; /** * Vacancies tab shows list from website * Created by Raza on 23-Nov-16. */ public class TabFragment1 extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { private final String list[] = { "Open in browser", "Copy", "Share", "Remove" }; private CustomCursorAdapter adapter; private ListView lvPosts; private SwipeRefreshLayout srLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab_fragment, container, false); lvPosts = (ListView) view.findViewById(R.id.lv_posts); srLayout = (SwipeRefreshLayout) view.findViewById(R.id.tab_fragment); fillData(); { lvPosts.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) { /** * This is magic right here wow, thanks stackexchange */ final Cursor c = (Cursor) parent.getAdapter().getItem(position); String name = c.getString(c.getColumnIndex(DbSchema.COL_NAME)); //Show dialog for choices AlertDialog.Builder inputDialog = new AlertDialog.Builder(getContext()); inputDialog.setTitle(name).setItems(list, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { menuOption(c, which); } }); inputDialog.show(); } }); lvPosts.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { /** * This is magic right here wow, thanks stackexchange */ final Cursor c = (Cursor) parent.getAdapter().getItem(position); String name = c.getString(c.getColumnIndex(DbSchema.COL_NAME)); //Show dialog for choices AlertDialog.Builder inputDialog = new AlertDialog.Builder(getContext()); inputDialog.setTitle(name).setItems(list, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { menuOption(c, which); } }); inputDialog.show(); return true; } }); } { srLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refresh(); } }); } return view; } private void fillData() { Log.i(getClass().getName(), "fill data"); getLoaderManager().initLoader(0, null, this); adapter = new CustomCursorAdapter(getContext(), null); lvPosts.setAdapter(adapter); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { Log.i(getClass().getName(), "creating loader"); CursorLoader cursorLoader = new CursorLoader(getContext(), NTSPostsContract.Posts.CONTENT_URI, NTSPostsContract.Posts.PROJECTION_ALL, null, null, NTSPostsContract.Posts.SORT_ORDER); return cursorLoader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { Log.i(getClass().getName(), "finished loader"); adapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { Log.i(getClass().getName(), "loader reset"); adapter.swapCursor(null); } private void menuOption(Cursor cursor, int selectedOption) { String name = cursor.getString(cursor.getColumnIndex(DbSchema.COL_NAME)); String url = cursor.getString(cursor.getColumnIndex(DbSchema.COL_URL)); switch (selectedOption) { //Open in browser case 0: { Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browse); break; } //Copy to clipboard case 1: { ClipboardManager clipboard = (ClipboardManager) getActivity() .getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText(name, url); clipboard.setPrimaryClip(clip); Toast.makeText(getContext(), "Link copied to clipboard", Toast.LENGTH_SHORT).show(); break; } //Share case 2: { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); startActivity(sendIntent); break; } case 3: { moveToIgnored(cursor); Toast.makeText(getContext(), name + "\nremoved to ignored posts", Toast.LENGTH_SHORT).show(); break; } } } private void moveToIgnored(Cursor cursor) { int id = cursor.getInt(cursor.getColumnIndex(DbSchema.COL_ID)); String name = cursor.getString(cursor.getColumnIndex(DbSchema.COL_NAME)); String url = cursor.getString(cursor.getColumnIndex(DbSchema.COL_URL)); String lastDate = cursor.getString(cursor.getColumnIndex(DbSchema.COL_LAST_DATE)); String creationDate = cursor.getString(cursor.getColumnIndex(DbSchema.COL_CREATION_DATE)); ContentValues values = new ContentValues(); values.put(DbSchema.COL_NAME, name); values.put(DbSchema.COL_URL, url); values.put(DbSchema.COL_LAST_DATE, lastDate); values.put(DbSchema.COL_CREATION_DATE, creationDate); Uri uri = getActivity().getContentResolver().insert(NTSPostsContract.Ignored.CONTENT_URI, values); if (uri != null) { getActivity().getContentResolver().delete(NTSPostsContract.Posts.CONTENT_URI, "_id = ?", new String[] { id + "", }); } } private void refresh() { if (MainActivity.isNetworkAvailable(getContext())) { doInBG(); } else { srLayout.setRefreshing(false); MainActivity.showSnackBar("No Internet Connection", getActivity()); } } private void doInBG() { new AsyncTask<Void, Void, String>() { Parser nts = new Parser(getContext()); @Override protected void onPreExecute() { srLayout.setRefreshing(true); } @Override protected String doInBackground(Void... params) { return nts.readWeb(); } @Override protected void onPostExecute(String s) { s = s.replaceAll("[^0-9]", ""); int i = Integer.parseInt(s); if (i == 200) { nts.saveToDB(); } else { new AlertDialog.Builder(getContext()).setTitle("Error!") .setMessage("Failed to read NTS website. Error code: " + i) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } srLayout.setRefreshing(false); } }.execute(); } }