summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/activity/OpmlImportActivity.java
blob: 6b59f919501cef4962301f814adabf27d34aedba (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
package de.danoeh.antennapod.activity;

import java.io.File;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.OpmlFeedQueuer;
import de.danoeh.antennapod.asynctask.OpmlImportWorker;
import de.danoeh.antennapod.opml.OpmlElement;
import de.danoeh.antennapod.util.StorageUtils;

/** Lets the user start the OPML-import process. */
public class OpmlImportActivity extends SherlockActivity {
	private static final String TAG = "OpmlImportActivity";

	public static final String IMPORT_DIR = "import/";

	private TextView txtvPath;
	private Button butStart;
	private String importPath;

	private OpmlImportWorker importWorker;

	private static ArrayList<OpmlElement> readElements;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.opml_import);

		txtvPath = (TextView) findViewById(R.id.txtvPath);
		butStart = (Button) findViewById(R.id.butStartImport);

		butStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkFolderForFiles();
			}

		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		StorageUtils.checkStorageAvailability(this);
		setImportPath();
	}

	/**
	 * Sets the importPath variable and makes txtvPath display the import
	 * directory.
	 */
	private void setImportPath() {
		File importDir = getExternalFilesDir(IMPORT_DIR);
		boolean success = true;
		if (!importDir.exists()) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Import directory doesn't exist. Creating...");
			success = importDir.mkdir();
			if (!success) {
				Log.e(TAG, "Could not create directory");
			}
		}
		if (success) {
			txtvPath.setText(importDir.toString());
			importPath = importDir.toString();
		} else {
			txtvPath.setText(R.string.opml_directory_error);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			return false;
		}
	}

	/**
	 * Looks at the contents of the import directory and decides what to do. If
	 * more than one file is in the directory, a dialog will be created to let
	 * the user choose which item to import
	 * */
	private void checkFolderForFiles() {
		File dir = new File(importPath);
		if (dir.isDirectory()) {
			File[] fileList = dir.listFiles();
			if (fileList.length == 1) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Found one file, choosing that one.");
				startImport(fileList[0]);
			} else if (fileList.length > 1) {
				Log.w(TAG, "Import directory contains more than one file.");
				askForFile(dir);
			} else {
				Log.e(TAG, "Import directory is empty");
				Toast toast = Toast
						.makeText(this, R.string.opml_import_error_dir_empty,
								Toast.LENGTH_LONG);
				toast.show();
			}
		}
	}

	/** Starts the import process. */
	private void startImport(File file) {

		if (file != null) {
			importWorker = new OpmlImportWorker(this, file) {

				@Override
				protected void onPostExecute(ArrayList<OpmlElement> result) {
					super.onPostExecute(result);
					if (result != null) {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Parsing was successful");
						readElements = result;
						startActivityForResult(new Intent(
								OpmlImportActivity.this,
								OpmlFeedChooserActivity.class), 0);
					} else {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Parser error occured");
					}
				}
			};
			importWorker.executeAsync();
		}
	}

	/**
	 * Asks the user to choose from a list of files in a directory and returns
	 * his choice.
	 */
	private void askForFile(File dir) {
		final File[] fileList = dir.listFiles();
		String[] fileNames = dir.list();

		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle(R.string.choose_file_to_import_label);
		dialog.setNeutralButton(android.R.string.cancel,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Dialog was cancelled");
						dialog.dismiss();
					}
				});
		dialog.setItems(fileNames, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "File at index " + which + " was chosen");
				dialog.dismiss();
				startImport(fileList[which]);
			}
		});
		dialog.create().show();
	}

	/**
	 * Handles the choices made by the user in the OpmlFeedChooserActivity and
	 * starts the OpmlFeedQueuer if necessary.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Received result");
		if (resultCode == RESULT_CANCELED) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Activity was cancelled");
		} else {
			int[] selected = data
					.getIntArrayExtra(OpmlFeedChooserActivity.EXTRA_SELECTED_ITEMS);
			if (selected != null && selected.length > 0) {
				OpmlFeedQueuer queuer = new OpmlFeedQueuer(this, selected) {

					@Override
					protected void onPostExecute(Void result) {
						super.onPostExecute(result);
						finish();
					}

				};
				queuer.executeAsync();
			} else {
				if (AppConfig.DEBUG)
					Log.d(TAG, "No items were selected");
			}
		}
	}

	public static ArrayList<OpmlElement> getReadElements() {
		return readElements;
	}

}