1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.external;
|
21
|
|
22
|
import java.io.File;
|
23
|
import java.io.FileInputStream;
|
24
|
import java.io.FileNotFoundException;
|
25
|
import java.io.FileOutputStream;
|
26
|
import java.io.IOException;
|
27
|
import java.io.InputStream;
|
28
|
import java.io.OutputStream;
|
29
|
import java.util.Locale;
|
30
|
|
31
|
import android.content.Context;
|
32
|
import android.graphics.Bitmap;
|
33
|
import android.graphics.BitmapFactory;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
|
37
|
public class RubikFiles
|
38
|
{
|
39
|
private static RubikFiles mThis;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
private RubikFiles()
|
44
|
{
|
45
|
|
46
|
}
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
// PUBLIC API
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
public static RubikFiles getInstance()
|
53
|
{
|
54
|
if( mThis==null ) mThis = new RubikFiles();
|
55
|
return mThis;
|
56
|
}
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
public InputStream openFile(Context context, String name)
|
61
|
{
|
62
|
File file = new File(context.getFilesDir(), name);
|
63
|
|
64
|
try
|
65
|
{
|
66
|
return new FileInputStream(file);
|
67
|
}
|
68
|
catch(FileNotFoundException ex)
|
69
|
{
|
70
|
android.util.Log.e("D", "file "+name+" not found: "+ex.getMessage());
|
71
|
}
|
72
|
|
73
|
return null;
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
public Bitmap getIcon(Context context, String name)
|
79
|
{
|
80
|
File dir = context.getFilesDir();
|
81
|
File file = new File(dir, name);
|
82
|
|
83
|
if( file.exists() )
|
84
|
{
|
85
|
String fname = dir.getAbsolutePath()+"/"+name;
|
86
|
return BitmapFactory.decodeFile(fname);
|
87
|
}
|
88
|
|
89
|
return null;
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
public boolean saveFile(Context context, InputStream stream, String name)
|
95
|
{
|
96
|
try
|
97
|
{
|
98
|
File file = new File(context.getFilesDir(), name);
|
99
|
OutputStream outStream = new FileOutputStream(file);
|
100
|
|
101
|
byte[] buffer = new byte[8*1024];
|
102
|
int bytesRead;
|
103
|
while ((bytesRead = stream.read(buffer)) != -1)
|
104
|
{
|
105
|
outStream.write(buffer, 0, bytesRead);
|
106
|
}
|
107
|
outStream.close();
|
108
|
|
109
|
return true;
|
110
|
}
|
111
|
catch(IOException ioe)
|
112
|
{
|
113
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
|
114
|
return false;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
public boolean saveIcon(Context context, Bitmap bmp, String name)
|
121
|
{
|
122
|
try
|
123
|
{
|
124
|
File file = new File(context.getFilesDir(), name);
|
125
|
OutputStream outStream = new FileOutputStream(file);
|
126
|
bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
|
127
|
outStream.close();
|
128
|
|
129
|
return true;
|
130
|
}
|
131
|
catch(Exception e)
|
132
|
{
|
133
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+e.getMessage() );
|
134
|
return false;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
public void deleteIcon(Context context, String name)
|
141
|
{
|
142
|
String filename = name.toLowerCase(Locale.ENGLISH) + ".png";
|
143
|
boolean success = context.deleteFile(filename);
|
144
|
if( !success ) android.util.Log.e("D", "failed to delete "+filename);
|
145
|
else android.util.Log.e("D", "successfully deleted "+filename);
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
|
150
|
public void deleteJsonObject(Context context, String name)
|
151
|
{
|
152
|
String filename = name.toLowerCase(Locale.ENGLISH) + "_object.json";
|
153
|
boolean success = context.deleteFile(filename);
|
154
|
if( !success ) android.util.Log.e("D", "failed to delete "+filename);
|
155
|
else android.util.Log.e("D", "successfully deleted "+filename);
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void deleteJsonExtras(Context context, String name)
|
161
|
{
|
162
|
String filename = name.toLowerCase(Locale.ENGLISH) + "_extras.json";
|
163
|
boolean success = context.deleteFile(filename);
|
164
|
if( !success ) android.util.Log.e("D", "failed to delete "+filename);
|
165
|
else android.util.Log.e("D", "successfully deleted "+filename);
|
166
|
}
|
167
|
}
|