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
|
|
30
|
import android.content.Context;
|
31
|
import android.graphics.Bitmap;
|
32
|
import android.graphics.BitmapFactory;
|
33
|
|
34
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
35
|
|
36
|
public class RubikFiles
|
37
|
{
|
38
|
private static RubikFiles mThis;
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
private RubikFiles()
|
43
|
{
|
44
|
|
45
|
}
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
// PUBLIC API
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
public static RubikFiles getInstance()
|
52
|
{
|
53
|
if( mThis==null ) mThis = new RubikFiles();
|
54
|
return mThis;
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
public InputStream openFile(Context context, String name)
|
60
|
{
|
61
|
File file = new File(context.getFilesDir(), name);
|
62
|
|
63
|
try
|
64
|
{
|
65
|
return new FileInputStream(file);
|
66
|
}
|
67
|
catch(FileNotFoundException ex)
|
68
|
{
|
69
|
android.util.Log.e("D", "file "+name+" not found: "+ex.getMessage());
|
70
|
}
|
71
|
|
72
|
return null;
|
73
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
public Bitmap getIcon(Context context, String name)
|
78
|
{
|
79
|
File dir = context.getFilesDir();
|
80
|
File file = new File(dir, name);
|
81
|
|
82
|
if( file.exists() )
|
83
|
{
|
84
|
String fname = dir.getAbsolutePath()+"/"+name;
|
85
|
return BitmapFactory.decodeFile(fname);
|
86
|
}
|
87
|
|
88
|
return null;
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public boolean saveFile(Context context, InputStream stream, String name)
|
94
|
{
|
95
|
try
|
96
|
{
|
97
|
File file = new File(context.getFilesDir(), name);
|
98
|
OutputStream outStream = new FileOutputStream(file);
|
99
|
|
100
|
byte[] buffer = new byte[8*1024];
|
101
|
int bytesRead;
|
102
|
while ((bytesRead = stream.read(buffer)) != -1)
|
103
|
{
|
104
|
outStream.write(buffer, 0, bytesRead);
|
105
|
}
|
106
|
outStream.close();
|
107
|
|
108
|
return true;
|
109
|
}
|
110
|
catch(IOException ioe)
|
111
|
{
|
112
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
|
113
|
return false;
|
114
|
}
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
public boolean saveIcon(Context context, Bitmap bmp, String name)
|
120
|
{
|
121
|
try
|
122
|
{
|
123
|
File file = new File(context.getFilesDir(), name);
|
124
|
OutputStream outStream = new FileOutputStream(file);
|
125
|
bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
|
126
|
outStream.close();
|
127
|
|
128
|
return true;
|
129
|
}
|
130
|
catch(Exception e)
|
131
|
{
|
132
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+e.getMessage() );
|
133
|
return false;
|
134
|
}
|
135
|
}
|
136
|
}
|