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

import org.shredzone.flattr4j.exception.FlattrException;

import android.content.Intent;
import android.net.Uri;
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 com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import de.danoeh.antennapod.util.FlattrUtils;
import de.danoeh.antennapod.R;

/** Guides the user through the authentication process */
public class FlattrAuthActivity extends SherlockActivity {
	private static final String TAG = "FlattrAuthActivity";

	private TextView txtvExplanation;
	private Button butAuthenticate;
	private Button butReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "Activity created");
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.flattr_auth);
		txtvExplanation = (TextView) findViewById(R.id.txtvExplanation);
		butAuthenticate = (Button) findViewById(R.id.but_authenticate);
		butReturn = (Button) findViewById(R.id.but_return_home);

		butReturn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(FlattrAuthActivity.this,
						MainActivity.class));
			}
		});
		
		butAuthenticate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					FlattrUtils.startAuthProcess(FlattrAuthActivity.this);
				} catch (FlattrException e) {
					e.printStackTrace();
				}
			}	
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "Activity resumed");
		Uri uri = getIntent().getData();
		if (uri != null) {
			Log.d(TAG, "Received uri");
			try {
				if (FlattrUtils.handleCallback(uri) != null) {
					handleAuthenticationSuccess();
					Log.d(TAG, "Authentication seemed to be successful");
				}
			} catch (FlattrException e) {
				e.printStackTrace();
			} 
		}
	}

	private void handleAuthenticationSuccess() {
		txtvExplanation.setText(R.string.flattr_auth_success);
		butAuthenticate.setEnabled(false);
		butReturn.setVisibility(View.VISIBLE);
	}

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

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			startActivity(new Intent(this, PreferenceActivity.class));
			break;
		default:
			return false;
		}
		return true;
	}

}