Commit 8c7952f0 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Storage permission is requested in runtime.

parent fa315715
package onlab.kvizserver;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
......@@ -10,6 +11,7 @@ import android.widget.Toast;
import com.google.gson.Gson;
import com.rustamg.filedialogs.FileDialog;
import com.rustamg.filedialogs.SaveFileDialog;
import java.io.BufferedReader;
import java.io.BufferedWriter;
......@@ -318,4 +320,22 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
questionListFragment.refreshQuestionList();
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
SaveFileDialog dialog = new SaveFileDialog();
dialog.show(getSupportFragmentManager(), SaveFileDialog.class.getName());
} else {
// permission denied
}
return;
}
}
}
}
package onlab.kvizserver;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
......@@ -56,8 +60,18 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
ChooseQuestionFileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileDialog dialog = new OpenFileDialog();
dialog.show(getSupportFragmentManager(), OpenFileDialog.class.getName());
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted; request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
// Permission has already been granted
FileDialog dialog = new OpenFileDialog();
dialog.show(getSupportFragmentManager(), OpenFileDialog.class.getName());
}
}
});
......@@ -133,4 +147,22 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
FileDialog dialog = new OpenFileDialog();
dialog.show(getSupportFragmentManager(), OpenFileDialog.class.getName());
} else {
// permission denied
}
return;
}
}
}
}
......@@ -9,6 +9,7 @@ import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
......@@ -66,11 +67,17 @@ public class QuestionListFragment extends Fragment {
exportButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
SaveFileDialog dialog = new SaveFileDialog();
dialog.show(getActivity().getSupportFragmentManager(), SaveFileDialog.class.getName());
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted; request the permission
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
// Permission has already been granted
SaveFileDialog dialog = new SaveFileDialog();
dialog.show(getActivity().getSupportFragmentManager(), SaveFileDialog.class.getName());
}
}
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment