Skip to content

Commit

Permalink
WIP readd Download Tab
Browse files Browse the repository at this point in the history
  • Loading branch information
fenimore committed Apr 2, 2021
1 parent 82ce811 commit 62c7c88
Show file tree
Hide file tree
Showing 46 changed files with 559 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package="com.workingagenda.democracydroid">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
*
* 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.Adapters;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.workingagenda.democracydroid.R;

import java.io.File;
import java.util.List;

/**
* Created by fen on 12/9/15.
*/
@SuppressWarnings("DefaultFileTemplate")
public class DownloadsAdapter extends ArrayAdapter<File> {

public DownloadsAdapter(Context context, int resource, List<File> files){
super(context, resource, files);
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent){
View v = convertView;

if(v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.row_download, null);
}

File f = getItem(position);
if (f != null) {
ImageView img = v.findViewById(R.id.row_image);
TextView txt = v.findViewById(R.id.row_title);
String title = f.getName();
Boolean isVideo = false;

if (txt != null && title.startsWith("dn")){
title = title.substring(0, title.length() - 4);
title = title.substring(2, title.length());
title = title.substring(0, 7) + "-" + title.substring(7, title.length());
if (title.endsWith("-1"))
title = title.substring(0, title.length()-2);
txt.setText(title);
} else if (txt != null) {
title = title.substring(0, title.length() - 12);
txt.setText(title);
}

if(f.getName().endsWith(".mp4")){
isVideo = true;
}
if (isVideo) {
img.setImageResource(R.drawable.ic_movie_icon);
} else {
img.setImageResource(R.drawable.ic_microphone);
}
}

return v;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
*/
package com.workingagenda.democracydroid.Adapters.ViewHolders;

import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
Expand All @@ -28,8 +35,11 @@
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;

Expand All @@ -38,6 +48,8 @@
import com.workingagenda.democracydroid.R;
import com.workingagenda.democracydroid.databinding.RowEpisodesBinding;

import static androidx.core.content.ContextCompat.*;

public class EpisodeViewHolder extends RecyclerView.ViewHolder
implements View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {

Expand All @@ -50,6 +62,7 @@ public class EpisodeViewHolder extends RecyclerView.ViewHolder
private final ImageView img;
private final TextView tag;
private final ImageView mOptions;
private final ImageView mDownload;
private final SharedPreferences preferences;
private Episode mEpisode;

Expand All @@ -61,6 +74,7 @@ public EpisodeViewHolder(final RowEpisodesBinding binding) {
tag = binding.rowEpisodesTag;
tag.setMaxLines(3);
mOptions = binding.rowEpisodesOptions;
mDownload = binding.rowDownload;
itemView.setOnCreateContextMenuListener(this);
preferences = PreferenceManager.getDefaultSharedPreferences(itemView.getContext());
}
Expand Down Expand Up @@ -92,8 +106,23 @@ public void showEpisode(final Episode e) {
}
itemView.setOnClickListener(view -> loadEpisode(e));
mOptions.setOnClickListener(view -> mOptions.showContextMenu());
}
mDownload.setOnClickListener(view -> {
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
builder.setTitle("Download");
builder.setMessage("Are you sure you want to download today's episode?");
builder.setNeutralButton("Cancel", (dialog, which) -> {});
builder.setNegativeButton("Audio", (dialog, which) ->
Download(e.getAudioUrl(), e.getTitle(), e.getDescription()));
builder.setPositiveButton("Video", (dialog, which) ->
Download(e.getVideoUrl(), e.getTitle(), e.getDescription()));
AlertDialog alert = builder.create();
alert.show();
}
);
}
}



private void loadEpisode(Episode e) {
if (e != null) {
Expand Down Expand Up @@ -155,6 +184,41 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
}
}


// FIXME: Show progress:
// http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog
private void Download(String url, String title, String desc) {
if (checkSelfPermission(itemView.getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
((Activity)itemView.getContext()).requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
0);
// TODO: catch onRequestPermissionsResult
} else {
if ("http://democracynow.videocdn.scaleengine.net/democracynow-iphone/play/democracynow/playlist.m3u8".equals(url)) {
Toast toast = Toast.makeText(itemView.getContext(),
"You can't download the Live Stream", Toast.LENGTH_LONG);
toast.show();
return;
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(desc);
request.setTitle(title);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

String fileext = url.substring(url.lastIndexOf('/') + 1);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PODCASTS, fileext);
//http://stackoverflow.com/questions/24427414/getsystemservices-is-undefined-when-called-in-a-fragment

// get download service and enqueue file
DownloadManager manager = (DownloadManager) itemView.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
// TODO: Save que ID for cancel button
Toast toast = Toast.makeText(itemView.getContext(), "Starting download of " + title, Toast.LENGTH_LONG);
toast.show();
}
}

@Override
public boolean onMenuItemClick(MenuItem menuItem) {
int DEFAULT_STREAM = Integer.parseInt(preferences.getString("pref_default_stream", "0")); // 0=video
Expand Down Expand Up @@ -204,6 +268,16 @@ else if (DEFAULT_STREAM == 0)
.setPositiveButton(android.R.string.ok, null)
.show();
return true;
case R.id.menu_video_download:
if (mEpisode.getTitle().equals("Stream Live"))
return true;
Download(mEpisode.getVideoUrl(), mEpisode.getTitle(), mEpisode.getDescription());
return true;
case R.id.menu_audio_download:
if (mEpisode.getTitle().equals("Stream Live"))
return true;
Download(mEpisode.getAudioUrl(), mEpisode.getTitle(), mEpisode.getDescription());
return true;
case R.id.menu_context_open_browser:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(mEpisode.getUrl()), "*/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.workingagenda.democracydroid.databinding.ActivityMainBinding;
import com.workingagenda.democracydroid.tabfragment.DownloadFragment;
import com.workingagenda.democracydroid.tabfragment.PodcastFragment;
import com.workingagenda.democracydroid.tabfragment.StoryFragment;

public class MainActivity extends AppCompatActivity {
private static final int POS_STORY = 0;
private static final int POS_PODCAST = 1;
private static final int TOTAL_COUNT = 2;
private static final int POS_DOWNLOAD = 2;
private static final int TOTAL_COUNT = 3;
ActivityMainBinding binding;

@Override
Expand Down Expand Up @@ -145,6 +147,8 @@ public Fragment createFragment(int position) {
return new StoryFragment();
case POS_PODCAST:
return new PodcastFragment();
case POS_DOWNLOAD:
return new DownloadFragment();
}
}

Expand All @@ -159,6 +163,9 @@ void getPageIcon(final TabLayout.Tab tab, final int position) {
tab.setIcon(R.drawable.ic_live_tv);
tab.setContentDescription(R.string.tab_broadcasts);
break;
case POS_DOWNLOAD:
tab.setIcon(R.drawable.ic_download_white);
tab.setContentDescription("Downloads");
}
}

Expand Down
Loading

0 comments on commit 62c7c88

Please sign in to comment.