1 |
35161021
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
806329e3
|
Leszek Koltunski
|
import java.io.File;
|
23 |
eb985085
|
Leszek Koltunski
|
import java.io.FileInputStream;
|
24 |
|
|
import java.io.FileNotFoundException;
|
25 |
806329e3
|
Leszek Koltunski
|
import java.io.FileOutputStream;
|
26 |
|
|
import java.io.IOException;
|
27 |
35161021
|
Leszek Koltunski
|
import java.io.InputStream;
|
28 |
806329e3
|
Leszek Koltunski
|
import java.io.OutputStream;
|
29 |
|
|
|
30 |
|
|
import android.content.Context;
|
31 |
|
|
import android.graphics.Bitmap;
|
32 |
7fe62d1f
|
Leszek Koltunski
|
import android.graphics.BitmapFactory;
|
33 |
806329e3
|
Leszek Koltunski
|
|
34 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
35 |
35161021
|
Leszek Koltunski
|
|
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 |
7fe62d1f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58 |
|
|
|
59 |
eb985085
|
Leszek Koltunski
|
public InputStream openFile(Context context, String name)
|
60 |
7fe62d1f
|
Leszek Koltunski
|
{
|
61 |
|
|
File file = new File(context.getFilesDir(), name);
|
62 |
|
|
|
63 |
eb985085
|
Leszek Koltunski
|
try
|
64 |
|
|
{
|
65 |
|
|
return new FileInputStream(file);
|
66 |
|
|
}
|
67 |
|
|
catch(FileNotFoundException ex)
|
68 |
|
|
{
|
69 |
|
|
// ignore
|
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 |
7fe62d1f
|
Leszek Koltunski
|
if( file.exists() )
|
83 |
|
|
{
|
84 |
eb985085
|
Leszek Koltunski
|
String fname = dir.getAbsolutePath()+"/"+name;
|
85 |
7fe62d1f
|
Leszek Koltunski
|
return BitmapFactory.decodeFile(fname);
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
return null;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
35161021
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92 |
|
|
|
93 |
806329e3
|
Leszek Koltunski
|
public boolean saveFile(Context context, InputStream stream, String name)
|
94 |
35161021
|
Leszek Koltunski
|
{
|
95 |
806329e3
|
Leszek Koltunski
|
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 |
7fe62d1f
|
Leszek Koltunski
|
public boolean saveIcon(Context context, Bitmap bmp, String name)
|
120 |
806329e3
|
Leszek Koltunski
|
{
|
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(IOException ioe)
|
131 |
|
|
{
|
132 |
|
|
android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
|
133 |
|
|
return false;
|
134 |
|
|
}
|
135 |
35161021
|
Leszek Koltunski
|
}
|
136 |
|
|
}
|