Skip to content

Commit 92bc45b

Browse files
committed
Ask user permission before update env
1 parent c421209 commit 92bc45b

File tree

7 files changed

+57
-16
lines changed

7 files changed

+57
-16
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
applicationId 'ru.meefik.linuxdeploy'
99
minSdkVersion 15
1010
targetSdkVersion 28
11-
versionCode 248
11+
versionCode 251
1212
versionName "2.4.0"
1313
}
1414
buildTypes {

app/src/main/java/ru/meefik/linuxdeploy/MainActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public void onCreate(Bundle savedInstanceState) {
118118
EnvUtils.execServices(getBaseContext(), new String[]{"telnetd", "httpd"}, "start");
119119
} else {
120120
// Update ENV
121-
new UpdateEnvTask(this).execute();
121+
PrefStore.setRepositoryUrl(this, getString(R.string.repository_url));
122+
updateEnvWithRequestPermissions();
122123
}
123124
}
124125

@@ -158,7 +159,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
158159
containerConfigure();
159160
break;
160161
case R.id.menu_export:
161-
containerExportWithRequestPermissions();
162+
containerExport();
162163
break;
163164
case R.id.menu_status:
164165
containerStatus();
@@ -405,14 +406,14 @@ private void openRepository() {
405406
/**
406407
* Request permission for write to storage
407408
*/
408-
private void containerExportWithRequestPermissions() {
409+
private void updateEnvWithRequestPermissions() {
409410
boolean hasPermission = (ContextCompat.checkSelfPermission(this,
410411
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
411412
if (!hasPermission) {
412413
ActivityCompat.requestPermissions(this,
413414
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
414415
} else {
415-
containerExport();
416+
new UpdateEnvTask(this).execute();
416417
}
417418
}
418419

@@ -422,7 +423,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
422423
switch (requestCode) {
423424
case REQUEST_WRITE_STORAGE: {
424425
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
425-
containerExport();
426+
new UpdateEnvTask(this).execute();
426427
} else {
427428
Toast.makeText(this, getString(R.string.write_permissions_disallow), Toast.LENGTH_LONG).show();
428429
}

app/src/main/java/ru/meefik/linuxdeploy/RepositoryActivity.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.io.InputStreamReader;
2727
import java.io.OutputStream;
2828
import java.lang.ref.WeakReference;
29+
import java.net.HttpURLConnection;
2930
import java.net.URL;
31+
import java.net.URLConnection;
3032
import java.util.ArrayList;
3133
import java.util.HashMap;
3234
import java.util.List;
@@ -159,6 +161,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
159161
Map profile = (Map) parent.getItemAtPosition(position);
160162
importDialog(profile);
161163
});
164+
165+
// Load list
166+
retrieveIndex();
162167
}
163168

164169
@Override
@@ -170,7 +175,6 @@ public void setTheme(int resId) {
170175
public void onResume() {
171176
super.onResume();
172177
setTitle(R.string.title_activity_repository);
173-
retrieveIndex();
174178
}
175179

176180
@Override
@@ -249,8 +253,26 @@ protected void onPostExecute(Boolean success) {
249253
private void downloadUrl(String url) throws IOException {
250254
BufferedReader reader = null;
251255
try {
252-
URL u = new URL(url + "/index.gz");
253-
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(u.openStream())));
256+
url = url + "/index.gz";
257+
HttpURLConnection conn;
258+
boolean redirect;
259+
do {
260+
redirect = false;
261+
conn = (HttpURLConnection) (new URL(url)).openConnection();
262+
// normally, 3xx is redirect
263+
int status = conn.getResponseCode();
264+
if (status != HttpURLConnection.HTTP_OK) {
265+
if (status == HttpURLConnection.HTTP_MOVED_TEMP
266+
|| status == HttpURLConnection.HTTP_MOVED_PERM
267+
|| status == HttpURLConnection.HTTP_SEE_OTHER)
268+
redirect = true;
269+
}
270+
if (redirect) {
271+
url = conn.getHeaderField("Location");
272+
}
273+
} while (redirect);
274+
conn.connect();
275+
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream())));
254276
String line;
255277
Map<String, String> map = new HashMap<>();
256278
while ((line = reader.readLine()) != null) {
@@ -331,8 +353,26 @@ private void downloadUrlAndImport(String url, String profile) throws IOException
331353
InputStream in = null;
332354
OutputStream out = null;
333355
try {
334-
URL u = new URL(new URL(url), "config/" + profile + ".conf");
335-
in = u.openStream();
356+
url = url + "/config/" + profile + ".conf";
357+
HttpURLConnection conn;
358+
boolean redirect;
359+
do {
360+
redirect = false;
361+
conn = (HttpURLConnection) (new URL(url)).openConnection();
362+
// normally, 3xx is redirect
363+
int status = conn.getResponseCode();
364+
if (status != HttpURLConnection.HTTP_OK) {
365+
if (status == HttpURLConnection.HTTP_MOVED_TEMP
366+
|| status == HttpURLConnection.HTTP_MOVED_PERM
367+
|| status == HttpURLConnection.HTTP_SEE_OTHER)
368+
redirect = true;
369+
}
370+
if (redirect) {
371+
url = conn.getHeaderField("Location");
372+
}
373+
} while (redirect);
374+
conn.connect();
375+
in = conn.getInputStream();
336376
out = new FileOutputStream(conf);
337377
byte[] buffer = new byte[1024];
338378
int read;

app/src/main/res/values/preferences.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<string name="autostart_delay" translatable="false">30</string>
1515
<string name="nettrack" translatable="false">false</string>
1616
<string name="env_dir" translatable="false"></string>
17-
<string name="repository_url" translatable="false">https://github.com/meefik/linuxdeploy-hub/raw/master/</string>
17+
<string name="repository_url" translatable="false">http://hub.meefik.ru</string>
1818
<string name="path" translatable="false"></string>
1919
<string name="is_cli" translatable="false">false</string>
2020
<string name="is_telnet" translatable="false">false</string>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
google()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.4.2'
9+
classpath 'com.android.tools.build:gradle:3.5.0'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Jun 18 22:22:14 MSK 2019
1+
#Fri Aug 23 00:13:36 MSK 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

0 commit comments

Comments
 (0)