Java tutorial
/* * * Copyright (C) 2014-2015 Fenimore Love * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package com.workingagenda.democracydroid; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.workingagenda.democracydroid.Adapters.DownloadsAdapter; import com.workingagenda.democracydroid.tabfragment.PodcastFragment; import com.workingagenda.democracydroid.tabfragment.StoryFragment; import java.io.File; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Shared Preferences SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); int DEFAULT_TAB = Integer.parseInt(preferences.getString("tab_preference", "1")); boolean PREF_FIRST_TIME = preferences.getBoolean("first_preference", true); // TODO: have splash screen for new users Log.d("First time", String.valueOf((PREF_FIRST_TIME))); // Tab Layouts TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_library_books_white_24dp)); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_live_tv_white_24dp)); tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_file_download_white_24dp)); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. /* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the sections. We use a {@link FragmentPagerAdapter} derivative, which will keep every loaded fragment in memory. If this becomes too memory intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = findViewById(R.id.container); mViewPager.setOffscreenPageLimit(2); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.setCurrentItem(DEFAULT_TAB); // Gather the Episode Lists // Set up the tab and View Pager mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); return true; } if (id == R.id.action_refresh) { // Don't let user click before async tasks are done item.setEnabled(false); // Call Fragment refresh methods getSupportFragmentManager().getFragments(); for (Fragment x : getSupportFragmentManager().getFragments()) { if (x instanceof PodcastFragment) ((PodcastFragment) x).refresh(); if (x instanceof StoryFragment) ((StoryFragment) x).refresh(); if (x instanceof DownloadFragment) ((DownloadFragment) x).refresh(); } // FIXME: Somehow enable this after async call... item.setEnabled(true); return true; } if (id == R.id.action_donate) { String url = "https://www.democracynow.org/donate"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); return true; } if (id == R.id.action_exclusives) { String url = "https://www.democracynow.org/categories/web_exclusive"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); return true; } if (id == R.id.action_site) { String url = "http://www.democracynow.org/"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); return true; } if (id == R.id.action_about) { Intent intent = new Intent(this, AboutActivity.class); startActivityForResult(intent, 0); } return super.onOptionsItemSelected(item); } /** * A Download fragment */ public static class DownloadFragment extends Fragment { public TextView Txt1; public Button btn; public Button btnRefresh; public ListView dList; public List<File> files; /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; public DownloadFragment() { } /** * Returns a new instance of this fragment for the given section * number. */ public static DownloadFragment newInstance(int sectionNumber) { DownloadFragment fragment = new DownloadFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_download, container, false); files = getListFiles(); dList = rootView.findViewById(android.R.id.list); Txt1 = rootView.findViewById(R.id.download_help); Txt1.setText(R.string.download_help); dList.setEmptyView(Txt1); btn = rootView.findViewById(R.id.clear); btnRefresh = rootView.findViewById(R.id.refresh); registerForContextMenu(dList); dList.setAdapter(new DownloadsAdapter(getContext(), R.layout.row_download, files)); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(getContext()).setTitle("Delete all downloads").setMessage( "Are you sure you want to delete all episodes?\nLong click and episode to delete them individually.") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { for (File file : files) { boolean delete = file.delete(); Log.d("File: ", file.getName() + String.valueOf(delete)); } files = getListFiles(); dList.setAdapter( new DownloadsAdapter(getContext(), R.layout.row_download, files)); Toast toast = Toast.makeText(getActivity(), "Downloads Removed", Toast.LENGTH_SHORT); toast.show(); } }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // do nothing } }).setIcon(android.R.drawable.ic_dialog_alert).show(); } }); btnRefresh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { files = getListFiles(); dList.setAdapter(new DownloadsAdapter(getContext(), R.layout.row_download, files)); } }); dList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { File f = files.get(position); Intent y = new Intent(getContext(), MediaActivity.class); y.putExtra("url", Uri.fromFile(f).toString()); //can't pass in article object? y.putExtra("title", f.getName()); startActivityForResult(y, 0); //Activity load = 0 } }); return rootView; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); if (v.getId() == android.R.id.list) { MenuInflater inflater = new MenuInflater(getContext()); menu.setHeaderTitle("Democracy Now!"); inflater.inflate(R.menu.download_menu, menu); } } @Override public boolean onContextItemSelected(MenuItem item) { //int pos = ; FIND A WAY TO PASS LiST ITEM POSITION? AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); if (info == null) return super.onContextItemSelected(item); int pos = info.position; File file = files.get(pos); switch (item.getItemId()) { case R.id.action_delete: boolean delete = file.delete(); Log.d("File: ", file.getName() + String.valueOf(delete)); files = getListFiles(); dList.setAdapter(new DownloadsAdapter(getContext(), R.layout.row_download, files)); return true; case R.id.action_external_player: Intent z = new Intent(Intent.ACTION_VIEW); z.setDataAndType(Uri.fromFile(file), "*/*"); startActivity(z); default: return super.onContextItemSelected(item); } } private List<File> getListFiles() { ArrayList<File> inFiles = new ArrayList<>(); File parentDir = new File(Environment.getExternalStorageDirectory().toString() + File.separator + Environment.DIRECTORY_PODCASTS); File[] files = parentDir.listFiles(); if (files != null) { for (File file : files) { if (file.getName().startsWith("dn") || file.getName().endsWith("-podcast.mp4") || file.getName().endsWith("-podcast.mp3")) { // there must be a smarter way to do this if (file.getName().endsWith(".mp3") || file.getName().endsWith(".mp4")) { inFiles.add(file); } } } } // Collections.reverse(inFiles); return inFiles; } private void refresh() { if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { files = getListFiles(); dList.setAdapter(new DownloadsAdapter(getContext(), R.layout.row_download, files)); } } } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PodcastFragment (defined as a static inner class below). // Return PodcastFragment.newInstance(position + 1); switch (position) { case 0: return StoryFragment.newInstance(position + 1); case 1: return PodcastFragment.newInstance(position + 1); case 2: return DownloadFragment.newInstance(position + 1); default: return PodcastFragment.newInstance(position + 1); } } @Override public int getCount() { // Show 3 total pages. return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "SECTION 1"; case 1: return "SECTION 2"; case 2: return "SECTION 3"; } return null; } } }