1+ package org .endlessos .key ;
2+
3+ import android .app .Activity ;
4+ import android .content .Intent ;
5+ import android .net .Uri ;
6+ import android .os .Bundle ;
7+ import android .util .Log ;
8+ import android .view .View ;
9+ import android .widget .Button ;
10+ import android .widget .Toolbar ;
11+
12+ import java .io .BufferedInputStream ;
13+ import java .io .File ;
14+ import java .io .FileNotFoundException ;
15+ import java .io .FileOutputStream ;
16+ import java .io .IOException ;
17+ import java .io .InputStream ;
18+ import java .util .zip .ZipEntry ;
19+ import java .util .zip .ZipException ;
20+ import java .util .zip .ZipInputStream ;
21+
22+ public class SettingsActivity extends Activity {
23+ // TODO: Add a way back to the main activity.
24+ // TODO: Restart the Kolibri service if it is running.
25+ // TODO: Show progress while importing.
26+ // TODO: Disable the import button while importing.
27+ // TODO: Display helpful information after importing.
28+
29+ private static final String TAG = Constants .TAG ;
30+
31+ private static final int CHOOSE_FILE_RESULT_CODE = 8778 ;
32+
33+ @ Override
34+ protected void onCreate (Bundle savedInstanceState ) {
35+ super .onCreate (savedInstanceState );
36+ setContentView (R .layout .activity_settings );
37+
38+ getActionBar ().setTitle ("Endless Key Settings" );
39+
40+ final Button importContentButton = findViewById (R .id .importContentButton );
41+ importContentButton .setOnClickListener (
42+ new View .OnClickListener () {
43+
44+ @ Override
45+ public void onClick (View view ) {
46+ Log .i ("importContent" , "onClick" );
47+ startImportContent ();
48+ }
49+ }
50+ );
51+ }
52+
53+ private void startImportContent () {
54+ Intent chooseFile = new Intent (Intent .ACTION_GET_CONTENT );
55+ chooseFile .addCategory (Intent .CATEGORY_OPENABLE );
56+ chooseFile .setType ("application/zip" );
57+ startActivityForResult (
58+ Intent .createChooser (chooseFile , "Choose a file" ),
59+ CHOOSE_FILE_RESULT_CODE
60+ );
61+ }
62+
63+ @ Override
64+ protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
65+ super .onActivityResult (requestCode , resultCode , data );
66+
67+ if (requestCode != CHOOSE_FILE_RESULT_CODE ) {
68+ return ;
69+ }
70+
71+ if (resultCode != Activity .RESULT_OK ) {
72+ return ;
73+ }
74+
75+ try {
76+ copyKolibriContent (
77+ data .getData (),
78+ KolibriUtils .getKolibriHome (getBaseContext ())
79+ );
80+ } catch (IOException e ) {
81+ e .printStackTrace ();
82+ }
83+ }
84+
85+ protected void copyKolibriContent (Uri sourceContent , File outputDirectory ) throws IOException {
86+ InputStream fileInput ;
87+
88+ fileInput = getContentResolver ().openInputStream (sourceContent );
89+
90+ ZipInputStream zipInput = new ZipInputStream (new BufferedInputStream (fileInput ));
91+ ZipEntry zipEntry = null ;
92+ byte [] buffer = new byte [1024 ];
93+
94+ while (true ) {
95+ String fileName ;
96+ File outputFile ;
97+
98+ zipEntry = zipInput .getNextEntry ();
99+
100+ if (zipEntry == null ) {
101+ break ;
102+ }
103+
104+ fileName = zipEntry .getName ();
105+
106+ if (!fileName .startsWith ("content/" )) {
107+ Log .d (TAG , "Content file has wrong parent directory: " + fileName );
108+ break ;
109+ }
110+
111+ if (fileName .equals ("content/manifest.json" )) {
112+ Long timeSeconds = System .currentTimeMillis () / 1000 ;
113+ fileName = "content/manifest." +timeSeconds +".json" ;
114+ }
115+
116+ outputFile = new File (outputDirectory , fileName );
117+
118+ if (zipEntry .isDirectory ()) {
119+ try {
120+ outputFile .mkdirs ();
121+ } catch (SecurityException error ) {
122+ error .printStackTrace ();
123+ }
124+ } else {
125+ int count ;
126+ FileOutputStream fileOutput ;
127+
128+ fileOutput = new FileOutputStream (outputFile );
129+
130+ while (true ) {
131+ count = zipInput .read (buffer );
132+
133+ if (count == -1 ) {
134+ break ;
135+ }
136+
137+ fileOutput .write (buffer , 0 , count );
138+ }
139+
140+ fileOutput .close ();
141+ zipInput .closeEntry ();
142+ }
143+ }
144+ }
145+ }
0 commit comments