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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.util.StorageUtils;

/** Is show if there is now external storage available. */
public class StorageErrorActivity extends ActionBarActivity {
	private static final String TAG = "StorageErrorActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(UserPreferences.getTheme());
		super.onCreate(savedInstanceState);

		setContentView(R.layout.storage_error);
	}

	@Override
	protected void onPause() {
		super.onPause();
		try {
			unregisterReceiver(mediaUpdate);
		} catch (IllegalArgumentException e) {

		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (StorageUtils.storageAvailable(this)) {
			leaveErrorState();
		} else {
			registerReceiver(mediaUpdate, new IntentFilter(
					Intent.ACTION_MEDIA_MOUNTED));
		}
	}

	private void leaveErrorState() {
		finish();
		startActivity(new Intent(this, MainActivity.class));
	}

	private BroadcastReceiver mediaUpdate = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
				if (intent.getBooleanExtra("read-only", true)) {
					if (BuildConfig.DEBUG)
						Log.d(TAG, "Media was mounted; Finishing activity");
					leaveErrorState();
				} else {
					if (BuildConfig.DEBUG)
						Log.d(TAG,
								"Media seemed to have been mounted read only");
				}
			}
		}

	};

}