diff --git a/src/main/java/info/guardianproject/phoneypot/MonitorActivity.java b/src/main/java/info/guardianproject/phoneypot/MonitorActivity.java index fc7a7fee..4188231a 100644 --- a/src/main/java/info/guardianproject/phoneypot/MonitorActivity.java +++ b/src/main/java/info/guardianproject/phoneypot/MonitorActivity.java @@ -8,9 +8,11 @@ */ package info.guardianproject.phoneypot; +import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.Camera; import android.os.Bundle; @@ -18,7 +20,9 @@ import android.os.Environment; import android.os.Handler; import android.support.design.widget.FloatingActionButton; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentActivity; +import android.support.v4.content.ContextCompat; import android.text.InputType; import android.util.Log; import android.view.DragEvent; @@ -57,9 +61,17 @@ public class MonitorActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - preferences = new PreferenceManager(getApplicationContext()); + super.onCreate(savedInstanceState); + + boolean permsNeeded = askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 1); + + if (!permsNeeded) + initLayout(); + } + + private void initLayout () + { + preferences = new PreferenceManager(getApplicationContext()); setContentView(R.layout.layout_running); txtTimer = (TextView)findViewById(R.id.timer_text); @@ -79,6 +91,14 @@ public void onClick(View v) { } }); + findViewById(R.id.timer_text_title).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (cTimer == null) + showNumberPicker(); + + } + }); findViewById(R.id.btnStartLater).setOnClickListener(new View.OnClickListener() { @Override @@ -111,7 +131,9 @@ public void onClick(View v) { } }); - } + + + } private void switchCamera () { @@ -236,9 +258,11 @@ private void initMonitor () private void close() { stopService(new Intent(this, MonitorService.class)); - preferences.unsetAccessToken(); - preferences.unsetDelegatedAccessToken(); - preferences.unsetPhoneId(); + if (preferences != null) { + preferences.unsetAccessToken(); + preferences.unsetDelegatedAccessToken(); + preferences.unsetPhoneId(); + } finish(); } @@ -277,4 +301,40 @@ public void onClick(DialogInterface dialogInterface, int i) { .show(); } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + switch (requestCode) { + case 1: + askForPermission(Manifest.permission.CAMERA,2); + break; + case 2: + initLayout(); + break; + } + + } + + + private boolean askForPermission(String permission, Integer requestCode) { + if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { + + // Should we show an explanation? + if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { + + //This is called if user has denied the permission before + //In this case I am just asking the permission again + ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode); + + } else { + + ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode); + } + return true; + } else { + return false; + } + } + } diff --git a/src/main/java/info/guardianproject/phoneypot/ui/CustomSlideNotify.java b/src/main/java/info/guardianproject/phoneypot/ui/CustomSlideNotify.java index 082a1ee3..8165f759 100644 --- a/src/main/java/info/guardianproject/phoneypot/ui/CustomSlideNotify.java +++ b/src/main/java/info/guardianproject/phoneypot/ui/CustomSlideNotify.java @@ -30,7 +30,7 @@ public class CustomSlideNotify extends Fragment { private static final String ARG_LAYOUT_RES_ID = "layoutResId"; private int layoutResId; private EditText mEditNumber; - + private View.OnClickListener mListener; public static CustomSlideNotify newInstance(int layoutResId) { CustomSlideNotify sampleSlide = new CustomSlideNotify(); @@ -41,6 +41,11 @@ public static CustomSlideNotify newInstance(int layoutResId) { return sampleSlide; } + public void setSaveListener (View.OnClickListener listener) + { + mListener = listener; + } + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -70,22 +75,14 @@ public void onClick(View v) { mEditNumber.setText(pm.getSmsNumber()); Button button = (Button)view.findViewById(R.id.btnSaveNumber); - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - saveNumber(); - } - }); + button.setOnClickListener(mListener); return view; } - private void saveNumber () + public String getPhoneNumber () { - PreferenceManager pm = new PreferenceManager(getActivity()); - pm.activateSms(true); - pm.setSmsNumber(mEditNumber.getText().toString()); - Toast.makeText(getActivity(),"Phone number saved!",Toast.LENGTH_SHORT).show(); + return mEditNumber.getText().toString(); } private void askForPermission(String permission, Integer requestCode) { diff --git a/src/main/java/info/guardianproject/phoneypot/ui/PPAppIntro.java b/src/main/java/info/guardianproject/phoneypot/ui/PPAppIntro.java index cdc284c9..45fbf4ea 100644 --- a/src/main/java/info/guardianproject/phoneypot/ui/PPAppIntro.java +++ b/src/main/java/info/guardianproject/phoneypot/ui/PPAppIntro.java @@ -8,8 +8,10 @@ import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.View; +import android.widget.Toast; +import info.guardianproject.phoneypot.PreferenceManager; import info.guardianproject.phoneypot.R; /** @@ -53,7 +55,17 @@ public void onClick(View v) { cs3.setTitle(getString(R.string.intro4_desc)); addSlide(cs3); - CustomSlideNotify cs4 = CustomSlideNotify.newInstance(R.layout.custom_slide_notify); + final CustomSlideNotify cs4 = CustomSlideNotify.newInstance(R.layout.custom_slide_notify); + cs4.setSaveListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + PreferenceManager pm = new PreferenceManager(PPAppIntro.this); + pm.activateSms(true); + pm.setSmsNumber(cs4.getPhoneNumber()); + Toast.makeText(PPAppIntro.this,"Phone number saved!",Toast.LENGTH_SHORT).show(); + getPager().setCurrentItem(getPager().getCurrentItem()+1); + } + }); addSlide(cs4); addSlide(AppIntroFragment.newInstance(getString(R.string.intro5_title), getString(R.string.intro5_desc),