1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.external;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.io.FileOutputStream;
|
16
|
import java.io.IOException;
|
17
|
import java.io.InputStream;
|
18
|
import java.io.OutputStream;
|
19
|
import java.util.Locale;
|
20
|
|
21
|
import android.content.Context;
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
public class RubikFiles
|
28
|
{
|
29
|
private static RubikFiles mThis;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
private RubikFiles()
|
34
|
{
|
35
|
|
36
|
}
|
37
|
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
// PUBLIC API
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
public static RubikFiles getInstance()
|
43
|
{
|
44
|
if( mThis==null ) mThis = new RubikFiles();
|
45
|
return mThis;
|
46
|
}
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
public InputStream openFile(Context context, String name)
|
51
|
{
|
52
|
File file = new File(context.getFilesDir(), name);
|
53
|
|
54
|
try
|
55
|
{
|
56
|
return new FileInputStream(file);
|
57
|
}
|
58
|
catch(FileNotFoundException ex)
|
59
|
{
|
60
|
android.util.Log.e("D", "file "+name+" not found: "+ex.getMessage());
|
61
|
}
|
62
|
|
63
|
return null;
|
64
|
}
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
public Bitmap getIcon(Context context, String name)
|
69
|
{
|
70
|
File dir = context.getFilesDir();
|
71
|
File file = new File(dir, name);
|
72
|
|
73
|
if( file.exists() )
|
74
|
{
|
75
|
String fname = dir.getAbsolutePath()+"/"+name;
|
76
|
return BitmapFactory.decodeFile(fname);
|
77
|
}
|
78
|
|
79
|
return null;
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
|
84
|
public boolean saveFile(Context context, InputStream stream, String name)
|
85
|
{
|
86
|
try
|
87
|
{
|
88
|
File file = new File(context.getFilesDir(), name);
|
89
|
OutputStream outStream = new FileOutputStream(file);
|
90
|
|
91
|
byte[] buffer = new byte[8*1024];
|
92
|
int bytesRead;
|
93
|
while ((bytesRead = stream.read(buffer)) != -1)
|
94
|
{
|
95
|
outStream.write(buffer, 0, bytesRead);
|
96
|
}
|
97
|
outStream.close();
|
98
|
|
99
|
return true;
|
100
|
}
|
101
|
catch(IOException ioe)
|
102
|
{
|
103
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
|
104
|
return false;
|
105
|
}
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
public boolean saveIcon(Context context, Bitmap bmp, String name)
|
111
|
{
|
112
|
try
|
113
|
{
|
114
|
File file = new File(context.getFilesDir(), name);
|
115
|
OutputStream outStream = new FileOutputStream(file);
|
116
|
bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
|
117
|
outStream.close();
|
118
|
|
119
|
return true;
|
120
|
}
|
121
|
catch(Exception e)
|
122
|
{
|
123
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+e.getMessage() );
|
124
|
return false;
|
125
|
}
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
public void deleteIcon(Context context, String name)
|
131
|
{
|
132
|
String filename = name.toLowerCase(Locale.ENGLISH) + ".png";
|
133
|
boolean success = context.deleteFile(filename);
|
134
|
if( !success ) android.util.Log.e("D", "failed to delete "+filename);
|
135
|
else android.util.Log.e("D", "successfully deleted "+filename);
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
public void deleteJsonObject(Context context, String name)
|
141
|
{
|
142
|
String filename = name.toLowerCase(Locale.ENGLISH) + "_object.json";
|
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 deleteJsonExtras(Context context, String name)
|
151
|
{
|
152
|
String filename = name.toLowerCase(Locale.ENGLISH) + "_extras.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
|
}
|