summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/activity/FeedInfoActivity.java
blob: fae8baf65aedb96d58f3289614b9a160958acff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package de.danoeh.antennapod.activity;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsSpinner;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

import com.joanzapata.android.iconify.Iconify;
import com.squareup.picasso.Picasso;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.LangUtils;
import de.danoeh.antennapod.menuhandler.FeedMenuHandler;

/**
 * Displays information about a feed.
 */
public class FeedInfoActivity extends ActionBarActivity {
    private static final String TAG = "FeedInfoActivity";
    private boolean autoDeleteChanged = false;

    public static final String EXTRA_FEED_ID = "de.danoeh.antennapod.extra.feedId";

    private Feed feed;

    private ImageView imgvCover;
    private TextView txtvTitle;
    private TextView txtvDescription;
    private TextView txtvLanguage;
    private TextView txtvAuthor;
    private TextView txtvUrl;
    private EditText etxtUsername;
    private EditText etxtPassword;
    private CheckBox cbxAutoDownload;
    private Spinner spnAutoDelete;

    private final View.OnClickListener copyUrlToClipboard = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(feed != null && feed.getDownload_url() != null) {
                String url = feed.getDownload_url();
                if (android.os.Build.VERSION.SDK_INT >= 11) {
                    ClipData clipData = ClipData.newPlainText(url, url);
                    android.content.ClipboardManager cm = (android.content.ClipboardManager) FeedInfoActivity.this
                            .getSystemService(Context.CLIPBOARD_SERVICE);
                    cm.setPrimaryClip(clipData);
                } else {
                    android.text.ClipboardManager cm = (android.text.ClipboardManager) FeedInfoActivity.this
                            .getSystemService(Context.CLIPBOARD_SERVICE);
                    cm.setText(url);
                }
                Toast t = Toast.makeText(FeedInfoActivity.this, R.string.copied_url_msg, Toast.LENGTH_SHORT);
                t.show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedinfo);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        long feedId = getIntent().getLongExtra(EXTRA_FEED_ID, -1);

        imgvCover = (ImageView) findViewById(R.id.imgvCover);
        txtvTitle = (TextView) findViewById(R.id.txtvTitle);
        txtvDescription = (TextView) findViewById(R.id.txtvDescription);
        txtvLanguage = (TextView) findViewById(R.id.txtvLanguage);
        txtvAuthor = (TextView) findViewById(R.id.txtvAuthor);
        txtvUrl = (TextView) findViewById(R.id.txtvUrl);
        cbxAutoDownload = (CheckBox) findViewById(R.id.cbxAutoDownload);
        spnAutoDelete = (Spinner) findViewById(R.id.spnAutoDelete);
        etxtUsername = (EditText) findViewById(R.id.etxtUsername);
        etxtPassword = (EditText) findViewById(R.id.etxtPassword);

        txtvUrl.setOnClickListener(copyUrlToClipboard);

        AsyncTask<Long, Void, Feed> loadTask = new AsyncTask<Long, Void, Feed>() {

            @Override
            protected Feed doInBackground(Long... params) {
                return DBReader.getFeed(FeedInfoActivity.this, params[0]);
            }

            @Override
            protected void onPostExecute(Feed result) {
                if (result != null) {
                    feed = result;
                    Log.d(TAG, "Language is " + feed.getLanguage());
                    Log.d(TAG, "Author is " + feed.getAuthor());
                    Log.d(TAG, "URL is " + feed.getDownload_url());
                    FeedPreferences prefs = feed.getPreferences();
                    imgvCover.post(new Runnable() {

                        @Override
                        public void run() {
                            Picasso.with(FeedInfoActivity.this)
                                    .load(feed.getImageUri())
                                    .fit()
                                    .into(imgvCover);
                        }
                    });

                    txtvTitle.setText(feed.getTitle());
                    String description = feed.getDescription();
                    txtvDescription.setText((description != null) ? description.trim() : "");
                    if (feed.getAuthor() != null) {
                        txtvAuthor.setText(feed.getAuthor());
                    }
                    if (feed.getLanguage() != null) {
                        txtvLanguage.setText(LangUtils
                                .getLanguageString(feed.getLanguage()));
                    }
                    txtvUrl.setText(feed.getDownload_url() + " {fa-paperclip}");
                            Iconify.addIcons(txtvUrl);

                    cbxAutoDownload.setEnabled(UserPreferences.isEnableAutodownload());
                    cbxAutoDownload.setChecked(prefs.getAutoDownload());
                    cbxAutoDownload.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                            feed.getPreferences().setAutoDownload(checked);
                            feed.savePreferences(FeedInfoActivity.this);
                        }
                    });
                    spnAutoDelete.setOnItemSelectedListener(new OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                            FeedPreferences.AutoDeleteAction auto_delete_action;
                            switch (parent.getSelectedItemPosition()) {
                                case 0:
                                    auto_delete_action = FeedPreferences.AutoDeleteAction.GLOBAL;
                                    break;

                                case 1:
                                    auto_delete_action = FeedPreferences.AutoDeleteAction.YES;
                                    break;

                                case 2:
                                    auto_delete_action = FeedPreferences.AutoDeleteAction.NO;
                                    break;

                                default: // TODO - add exceptions here
                                    return;
                            }
                            feed.getPreferences().setAutoDeleteAction(auto_delete_action);// p
                            autoDeleteChanged = true;
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            // Another interface callback
                        }
                    });
                    spnAutoDelete.setSelection(prefs.getAutoDeleteAction().ordinal());

                    etxtUsername.setText(prefs.getUsername());
                    etxtPassword.setText(prefs.getPassword());

                    etxtUsername.addTextChangedListener(authTextWatcher);
                    etxtPassword.addTextChangedListener(authTextWatcher);

                    supportInvalidateOptionsMenu();

                } else {
                    Log.e(TAG, "Activity was started with invalid arguments");
                }
            }
        };
        loadTask.execute(feedId);
    }


    private boolean authInfoChanged = false;

    private TextWatcher authTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            authInfoChanged = true;
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        if (feed != null) {
            FeedPreferences prefs = feed.getPreferences();
            if (authInfoChanged) {
                Log.d(TAG, "Auth info changed, saving credentials");
                prefs.setUsername(etxtUsername.getText().toString());
                prefs.setPassword(etxtPassword.getText().toString());
            }
            if (authInfoChanged || autoDeleteChanged) {
                DBWriter.setFeedPreferences(this, prefs);
            }
            authInfoChanged = false;
            autoDeleteChanged = false;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.feedinfo, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.findItem(R.id.support_item).setVisible(
                feed != null && feed.getPaymentLink() != null);
        menu.findItem(R.id.share_link_item).setVisible(feed != null && feed.getLink() != null);
        menu.findItem(R.id.visit_website_item).setVisible(feed != null && feed.getLink() != null &&
                IntentUtils.isCallable(this, new Intent(Intent.ACTION_VIEW, Uri.parse(feed.getLink()))));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                try {
                    return FeedMenuHandler.onOptionsItemClicked(this, item, feed);
                } catch (DownloadRequestException e) {
                    e.printStackTrace();
                    DownloadRequestErrorDialogCreator.newRequestErrorDialog(this,
                            e.getMessage());
                }
                return super.onOptionsItemSelected(item);
        }
    }
}