package jp.sourceforge.gokigen.memoma;
+import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import jp.sourceforge.gokigen.memoma.fileio.MeMoMaDataInOutManager;
+import static android.os.Build.VERSION_CODES.KITKAT;
+
/**
* メイン画面の処理
*
setContentView(R.layout.main);
// 外部メモリアクセス権のオプトイン
- if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
- ActivityCompat.requestPermissions(this,
- new String[]{
- Manifest.permission.WRITE_EXTERNAL_STORAGE,
- },
- REQUEST_NEED_PERMISSIONS);
+ if (((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED))||
+ (ContextCompat.checkSelfPermission(this, Manifest.permission.MANAGE_DOCUMENTS) != PackageManager.PERMISSION_GRANTED))
+ {
+ if (Build.VERSION.SDK_INT >= KITKAT)
+ {
+ ActivityCompat.requestPermissions(this,
+ new String[]{
+ Manifest.permission.WRITE_EXTERNAL_STORAGE,
+ Manifest.permission.MANAGE_DOCUMENTS,
+ },
+ REQUEST_NEED_PERMISSIONS);
+ }
+ else
+ {
+ ActivityCompat.requestPermissions(this,
+ new String[]{
+ Manifest.permission.WRITE_EXTERNAL_STORAGE,
+ },
+ REQUEST_NEED_PERMISSIONS);
+ }
}
try
package jp.sourceforge.gokigen.memoma.preference;
+import android.app.Activity;
+import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
+import android.provider.MediaStore;
+import android.support.annotation.NonNull;
+import android.util.Log;
import jp.sourceforge.gokigen.memoma.R;
* @author MRSa
*
*/
-public class Preference extends PreferenceActivity implements OnSharedPreferenceChangeListener
+public class Preference extends PreferenceActivity implements OnSharedPreferenceChangeListener, android.preference.Preference.OnPreferenceClickListener
{
+ private final String TAG = toString();
+ private final IPreferenceIntentCaller intentCaller;
+ private final int SELECT_BACKGROUND_IMAGE = 100;
+
/**
* コンストラクタ
*
**/
public Preference()
{
-
+ intentCaller = PreferenceIntentCaller.newInstance(this);
}
@Override
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preference);
+
+ // findPreference("backgroundUri").setOnPreferenceClickListener(this);
}
@Override
{
//
}
+
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, @NonNull Intent data)
+ {
+ super.onActivityResult(requestCode, resultCode, data);
+ Log.v(TAG, "onActivityResult() : start");
+ try
+ {
+ switch (resultCode)
+ {
+ case Activity.RESULT_OK:
+ String filePath = "";
+ String[] projection = {MediaStore.MediaColumns.DATA};
+ try
+ {
+ Cursor cursor = getContentResolver().query(data.getData(), projection, null, null, null);
+ if (cursor != null) {
+ if (cursor.getCount() > 0) {
+ cursor.moveToNext();
+ filePath = cursor.getString(0);
+ }
+ cursor.close();
+ }
+ setActivityResultValue(requestCode, filePath);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ Log.v(TAG, "onActivityResult() : end");
+ }
+
+ private void setActivityResultValue(int requestCode, String filePath)
+ {
+ String key;
+ if (requestCode == SELECT_BACKGROUND_IMAGE)
+ {
+ key = "backgroundUri";
+ }
+ else
+ {
+ // 何もしない
+ return;
+ }
+ SharedPreferences preferences = getPreferenceScreen().getSharedPreferences();
+ String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
+ findPreference(key).setSummary(fileName);
+ SharedPreferences.Editor editor = preferences.edit();
+ editor.putString(key, filePath);
+ editor.apply();
+ Log.v(TAG, " key : " + key + " image File : " + fileName + " (" + filePath + ")");
+ //Toast.makeText(getContext(), "Selected :" + fileName, Toast.LENGTH_SHORT).show();
+ }
+
+ @Override
+ public boolean onPreferenceClick(android.preference.Preference preference)
+ {
+ try
+ {
+ if (!preference.hasKey())
+ {
+ return (false);
+ }
+ String key = preference.getKey();
+ return (key.contains("backgroundUri") && (intentCaller.selectBackgroundImageFileFromGallery(SELECT_BACKGROUND_IMAGE)));
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ return (false);
+ }
}
--- /dev/null
+package jp.sourceforge.gokigen.memoma.preference;
+
+import android.app.Activity;
+import android.content.Intent;
+
+public class PreferenceIntentCaller implements IPreferenceIntentCaller
+{
+ private final Activity activity;
+
+ public static IPreferenceIntentCaller newInstance(Activity activity)
+ {
+ return (new PreferenceIntentCaller(activity));
+ }
+
+ private PreferenceIntentCaller(Activity activity)
+ {
+ this.activity = activity;
+ }
+
+
+ @Override
+ public boolean selectBackgroundImageFileFromGallery(int code)
+ {
+ Intent intent = new Intent(Intent.ACTION_PICK);
+ intent.setType("image/*");
+ activity.startActivityForResult(intent, code);
+ return (false);
+ }
+}