6
6
import java .io .FileOutputStream ;
7
7
import java .io .IOException ;
8
8
import java .io .InputStreamReader ;
9
- import java .nio .channels .FileChannel ;
10
9
import java .util .Date ;
10
+ import java .util .Map ;
11
11
import java .util .Properties ;
12
12
13
13
import android .app .Activity ;
14
+ import android .content .Context ;
14
15
import android .content .Intent ;
15
- import android .database . Cursor ;
16
+ import android .content . SharedPreferences ;
16
17
import android .graphics .Bitmap ;
17
18
import android .graphics .BitmapFactory ;
18
19
import android .graphics .drawable .BitmapDrawable ;
19
20
import android .graphics .drawable .Drawable ;
20
- import android .net .Uri ;
21
21
import android .os .Bundle ;
22
- import android .provider . MediaStore ;
22
+ import android .preference . PreferenceManager ;
23
23
import android .support .v4 .view .ViewPager ;
24
24
import android .util .DisplayMetrics ;
25
25
import android .util .Log ;
@@ -32,99 +32,27 @@ public class AndroidZenWriterActivity extends Activity {
32
32
public static String currentFilename = "current.txt" ;
33
33
public static String settingsFilename = "settings.properties" ;
34
34
35
- Properties settings = new Properties ();
36
-
37
35
public static final int SELECT_PHOTO = 100 ;
38
36
39
37
/** Called when the activity is first created. */
40
38
@ Override
41
39
public void onCreate (Bundle savedInstanceState ) {
42
40
super .onCreate (savedInstanceState );
43
- loadSettings ();
41
+ // loadSettings();
44
42
setContentView (R .layout .main );
45
43
ViewPager pager = (ViewPager ) findViewById (R .id .ViewPager1 );
46
44
pager .setAdapter (new ZenAdapter (this ));
47
45
pager .setCurrentItem (1 , true );
48
- View top = findViewById (R .id .Top );
49
- if (settings .containsKey ("backgroundImage" )) {
50
- Drawable background = getDrawable (settings .getProperty ("backgroundImage" ));
51
- if (background != null ) {
52
- top .setBackgroundDrawable (background );
53
- }
54
- }
46
+
47
+ applyPreferences ();
48
+
55
49
}
56
50
57
- protected void onActivityResult (int requestCode , int resultCode ,
58
- Intent imageReturnedIntent ) {
59
- super .onActivityResult (requestCode , resultCode , imageReturnedIntent );
60
-
61
- switch (requestCode ) {
62
- case SELECT_PHOTO :
63
- if (resultCode == RESULT_OK ) {
64
- Uri selectedImage = imageReturnedIntent .getData ();
65
-
66
- Log .i ("SelectedImage" , selectedImage .toString ());
67
- View top = findViewById (R .id .Top );
68
- String path = getPath (selectedImage );
69
- Log .i ("SelectedImage" , "File Path: " + path );
70
- File selectedImageFile = new File (path );
71
-
72
- try {
73
-
74
- File destFile = getFileStreamPath (selectedImageFile
75
- .getName ());
76
-
77
- if (!destFile .exists () && selectedImageFile .exists ()) {
78
- Log .i ("SelectedImage" , "Copying file: "
79
- + selectedImageFile .getAbsolutePath ());
80
- FileChannel src = new FileInputStream (selectedImageFile )
81
- .getChannel ();
82
- FileChannel dst = openFileOutput (
83
- selectedImageFile .getName (), MODE_PRIVATE )
84
- .getChannel ();
85
- dst .transferFrom (src , 0 , src .size ());
86
- src .close ();
87
- dst .close ();
88
- Log .i ("SelectedImage" , "Copy Complete!" );
89
- Log .i ("SelectedImage" ,
90
- "Destination File: "
91
- + destFile .getAbsolutePath ());
92
- } else {
93
- Log .i ("SelectedImage" ,
94
- "The file was already in our private storage: "
95
- + destFile .getAbsolutePath ());
96
- }
97
- Toast .makeText (this ,
98
- "Copied image: " + selectedImageFile .getName (),
99
- Toast .LENGTH_LONG ).show ();
100
- } catch (Exception e ) {
101
- Log .e ("SelectedImage" , "Failed to Copy Image" , e );
102
- }
103
-
104
- Drawable drawable = getDrawable (selectedImageFile .getName ());
105
- if (drawable != null ) {
106
- top .setBackgroundDrawable (drawable );
107
- settings .put ("backgroundImage" , selectedImageFile .getName ());
108
- saveSettings ();
109
- }
110
-
111
- }
112
- }
113
- }
114
-
115
- public String getPath (Uri uri ) {
116
- String [] projection = { MediaStore .Images .Media .DATA };
117
- Cursor cursor = managedQuery (uri , projection , null , null , null );
118
- startManagingCursor (cursor );
119
- int column_index = cursor
120
- .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
121
- cursor .moveToFirst ();
122
- return cursor .getString (column_index );
123
- }
51
+ public static final int EDIT_PREFERENCES = 1 ;
124
52
125
53
public void openThemeSettings (View parent ) {
126
54
Intent settingsActivity = new Intent (this , PrefsActivity .class );
127
- startActivity (settingsActivity );
55
+ startActivityForResult (settingsActivity , EDIT_PREFERENCES );
128
56
}
129
57
130
58
@ Override
@@ -136,7 +64,7 @@ protected void onPause() {
136
64
@ Override
137
65
protected void onDestroy () {
138
66
super .onDestroy ();
139
- saveSettings ();
67
+ // saveSettings();
140
68
saveFile (currentFilename );
141
69
}
142
70
@@ -196,7 +124,31 @@ protected void loadFile(String filename) {
196
124
}
197
125
198
126
}
199
-
127
+
128
+
129
+ protected void applyPreferences () {
130
+ View top = findViewById (R .id .Top );
131
+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (this );
132
+ String backgroundImage = prefs .getString ("backgroundpref" , "" );
133
+ Log .i ("AndroidZenWriter:applyPreferences" , "BackgroundImage: " + backgroundImage );
134
+ if (!backgroundImage .equals ("" )) {
135
+ Drawable background = getDrawable (this , backgroundImage );
136
+ if (background != null ) {
137
+ top .setBackgroundDrawable (background );
138
+ }
139
+ }
140
+ // TODO: Apply other preferences
141
+ }
142
+
143
+ protected void onActivityResult (int requestCode , int resultCode ,
144
+ Intent intent ) {
145
+ super .onActivityResult (requestCode , resultCode , intent );
146
+ if (requestCode == EDIT_PREFERENCES && resultCode == RESULT_OK ) {
147
+ this .applyPreferences ();
148
+ }
149
+
150
+ }
151
+ /*
200
152
public void saveSettings() {
201
153
FileOutputStream fos = null;
202
154
@@ -237,16 +189,13 @@ public void loadSettings() {
237
189
}
238
190
Toast.makeText(this, "Loaded settings", Toast.LENGTH_LONG).show();
239
191
}
240
-
241
- public Drawable getDrawable (String filename ) {
192
+ */
193
+ public static Drawable getDrawable (Context context , String filename ) {
242
194
243
195
BitmapFactory .Options opts = new BitmapFactory .Options ();
244
- DisplayMetrics metrics = new DisplayMetrics ();
245
- getWindowManager ().getDefaultDisplay ().getMetrics (metrics );
246
- opts .inTargetDensity = metrics .densityDpi ;
247
196
opts .inSampleSize = 2 ;
248
197
Bitmap backgroundBitmap = BitmapFactory .decodeFile (
249
- getFileStreamPath (filename ).getAbsolutePath (), opts );
198
+ context . getFileStreamPath (filename ).getAbsolutePath (), opts );
250
199
251
200
if (backgroundBitmap != null ) {
252
201
BitmapDrawable drawable = new BitmapDrawable (backgroundBitmap );
0 commit comments