summaryrefslogtreecommitdiff
path: root/src/org/fox/ttcomics/CommonActivity.java
blob: 97147d92aef80a3830ccb5a4ccdc3706438c13a0 (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
package org.fox.ttcomics;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Display;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
import android.widget.Toast;

public class CommonActivity extends FragmentActivity {
	private final String TAG = this.getClass().getSimpleName();

	protected static final String FRAG_COMICS_PAGER = "comic_pager";
	protected static final String FRAG_COMICS_LIST = "comics_list";

	protected SharedPreferences m_prefs;

	private boolean m_smallScreenMode = true;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    	m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        
	}

	protected void setSmallScreen(boolean smallScreen) {
		Log.d(TAG, "m_smallScreenMode=" + smallScreen);
		m_smallScreenMode = smallScreen;
	}

	public boolean isSmallScreen() {
		return m_smallScreenMode;
	}

	
	public void onComicArchiveSelected(String fileName) {
		//
	}

	public void setSize(String fileName, int size) {
		SharedPreferences lastread = getSharedPreferences("lastread", 0);

		SharedPreferences.Editor editor = lastread.edit();    	
    	editor.putInt(fileName + ":size", size);
    	
    	editor.commit();

	}
	
	public void setLastPosition(String fileName, int position) {
    	SharedPreferences lastread = getSharedPreferences("lastread", 0);
    	
    	int lastPosition = getLastPosition(fileName);
    	
    	SharedPreferences.Editor editor = lastread.edit();    	
    	editor.putInt(fileName + ":last", position);
    	editor.putInt(fileName + ":max", Math.max(lastPosition, position));
    	
    	editor.commit();
	}
	
	public void setLastMaxPosition(String fileName, int position) {
    	SharedPreferences lastread = getSharedPreferences("lastread", 0);
    	
    	SharedPreferences.Editor editor = lastread.edit();    	
    	editor.putInt(fileName + ":max", position);
    	
    	editor.commit();
	}
	
	public int getLastPosition(String fileName) { 
		SharedPreferences lastread = getSharedPreferences("lastread", 0);
	
		return lastread.getInt(fileName + ":last", 0);
	}

	public int getMaxPosition(String fileName) { 
		SharedPreferences lastread = getSharedPreferences("lastread", 0);
	
		return lastread.getInt(fileName + ":max", 0);
	}

	public int getSize(String fileName) { 
		SharedPreferences lastread = getSharedPreferences("lastread", 0);
	
		return lastread.getInt(fileName + ":size", -1);
	}

    public void onComicSelected(String fileName, int position) {
    	setLastPosition(fileName, position);
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_rescan:
			ComicListFragment frag = (ComicListFragment) getSupportFragmentManager().findFragmentByTag(FRAG_COMICS_LIST);
			
			if (frag != null && frag.isAdded()) {
				frag.rescan(true);
			}
			
			return true;
		case R.id.menu_settings:
			Intent intent = new Intent(CommonActivity.this,
					PreferencesActivity.class);
			startActivityForResult(intent, 0);
			return true;
		default:
			Log.d(TAG,
					"onOptionsItemSelected, unhandled id=" + item.getItemId());
			return super.onOptionsItemSelected(item);
		}
	}

	@SuppressWarnings("deprecation")
	public boolean isPortrait() {
		Display display = getWindowManager().getDefaultDisplay(); 
		
	    int width = display.getWidth();
	    int height = display.getHeight();
		
	    return width < height;
	}

	protected static String md5(String s) {
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();
	        
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        
	        return hexString.toString();
	        
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public String getCacheFileName(String fileName) {
		String hash = md5(fileName);
		
		File file = new File(getExternalCacheDir().getAbsolutePath() + "/" + hash + ".png");
		
		return file.getAbsolutePath();
	}
	
	public void toast(int msgId) {
		Toast toast = Toast.makeText(CommonActivity.this, msgId, Toast.LENGTH_SHORT);
		toast.show();
	}

	public void toast(String msg) {
		Toast toast = Toast.makeText(CommonActivity.this, msg, Toast.LENGTH_SHORT);
		toast.show();
	}

}