SHARE
TWEET

Appsee Camera/Permission Session Stitching

a guest Dec 6th, 2016 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.appseelifecycle;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.graphics.Bitmap;
  8. import android.os.Build;
  9. import android.os.Handler;
  10. import android.os.HandlerThread;
  11. import android.provider.MediaStore;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.Toast;
  17.  
  18. import com.appsee.Appsee;
  19. import com.appsee.AppseeListener;
  20. import com.appsee.AppseeScreenDetectedInfo;
  21. import com.appsee.AppseeSessionEndedInfo;
  22. import com.appsee.AppseeSessionEndingInfo;
  23. import com.appsee.AppseeSessionStartedInfo;
  24. import com.appsee.AppseeSessionStartingInfo;
  25.  
  26. import java.io.FileOutputStream;
  27.  
  28. public class MainActivity extends AppCompatActivity
  29. {
  30.     private static final String TAG = "AppseeDemo";
  31.     private static final int TAKE_PICTURE_REQUEST_CODE = 98;
  32.     private static final int PERMISSION_REQUEST_CODE = 99;
  33.     private static AppseeListener appseeListener = null;
  34.  
  35.     // Indicates if app is not foreground but should keep appsee session alive (until the timeout or app becomes foreground again)
  36.     private static boolean appseeSessionKeepAlive = false;
  37.  
  38.     // Handler that will call Appsee.finishSession() upon timeout
  39.     private static Handler handler;
  40.  
  41.     // Sets how much time appsee session continues after app is no longer in foreground
  42.     private static final int MAX_APPSEE_SESSION_TIMEOUT = 5 * 1000; // 5 seconds
  43.  
  44.     // Runnable that forces appsee to finish the session now
  45.     private static final Runnable appseeForceFinishSession = new Runnable()
  46.     {
  47.         @Override
  48.         public void run()
  49.         {
  50.             // Must allow the session to end now after timeout!
  51.             setAppseeSessionKeepAlive(false);
  52.  
  53.             // Finishing session due to too much time outside the app
  54.             Appsee.finishSession(true, true);
  55.         }
  56.     };
  57.  
  58.     @Override
  59.     protected void onCreate(Bundle savedInstanceState)
  60.     {
  61.         super.onCreate(savedInstanceState);
  62.         setContentView(R.layout.activity_main);
  63.  
  64.         findViewById(R.id.btnTakePhoto).setOnClickListener(new View.OnClickListener()
  65.         {
  66.             @Override
  67.             public void onClick(View view)
  68.             {
  69.                 startCamera();
  70.             }
  71.         });
  72.  
  73.         startAppsee();
  74.     }
  75.  
  76.     private void startAppsee()
  77.     {
  78.         if (appseeListener == null)
  79.         {
  80.             appseeListener = new AppseeListener()
  81.             {
  82.                 @Override
  83.                 public void onAppseeSessionEnding(AppseeSessionEndingInfo appseeSessionEndingInfo)
  84.                 {
  85.                     appseeSessionEndingInfo.setShouldEndSession(!appseeSessionKeepAlive);
  86.                 }
  87.  
  88.                 @Override
  89.                 public void onAppseeSessionStarting(AppseeSessionStartingInfo appseeSessionStartingInfo) {}
  90.  
  91.                 @Override
  92.                 public void onAppseeSessionStarted(AppseeSessionStartedInfo appseeSessionStartedInfo) {}
  93.  
  94.                 @Override
  95.                 public void onAppseeSessionEnded(AppseeSessionEndedInfo appseeSessionEndedInfo) {}
  96.  
  97.                 @Override
  98.                 public void onAppseeScreenDetected(AppseeScreenDetectedInfo appseeScreenDetectedInfo) {}
  99.             };
  100.  
  101.             Appsee.addAppseeListener(appseeListener);
  102.         }
  103.  
  104.         HandlerThread handlerThread = new HandlerThread("AppseeDelayedSessionClosingThread");
  105.         handlerThread.start();
  106.         handler = new Handler(handlerThread.getLooper());
  107.  
  108.         Appsee.start("API KEY");
  109.     }
  110.  
  111.     private void startCamera()
  112.     {
  113.         // Must ask for permission on Android M and up!
  114.         askPermission();
  115.  
  116.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
  117.         {
  118.             Log.w(TAG, "No permission!");
  119.             return;
  120.         }
  121.  
  122.         // before potentially leaving the app, flagging for appsee to avoid ending session
  123.         setAppseeSessionKeepAlive(true);
  124.         startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE_REQUEST_CODE);
  125.     }
  126.  
  127.     @Override
  128.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
  129.     {
  130.         // Back in the app, now appsee can control the session lifecycle again
  131.         setAppseeSessionKeepAlive(false);
  132.  
  133.         if (requestCode == TAKE_PICTURE_REQUEST_CODE && resultCode == Activity.RESULT_OK)
  134.         {
  135.             if (data.getExtras().containsKey("data"))
  136.             {
  137.                 Bitmap bitmap = (Bitmap)data.getExtras().get("data");
  138.  
  139.                 if (bitmap != null)
  140.                 {
  141.                     try
  142.                     {
  143.                         FileOutputStream fos = new FileOutputStream("/sdcard/test.jpg");
  144.                         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  145.                         fos.close();
  146.                     }
  147.                     catch (Exception e)
  148.                     {
  149.                         Log.e(TAG, "Error saving image, Exception" + e);
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.     }
  155.  
  156.     private void askPermission()
  157.     {
  158.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
  159.  
  160.         if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
  161.         {
  162.             if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE))
  163.             {
  164.                 Toast.makeText(this, "Must need permission to save the file", Toast.LENGTH_LONG).show();
  165.             }
  166.  
  167.             // before potentially leaving the app, flagging for appsee to avoid ending session
  168.             setAppseeSessionKeepAlive(true);
  169.             requestPermissions(new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, PERMISSION_REQUEST_CODE);
  170.         }
  171.     }
  172.  
  173.     @Override
  174.     public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
  175.     {
  176.         // Back in the app, now appsee can control the session lifecycle again
  177.         setAppseeSessionKeepAlive(false);
  178.  
  179.         switch (requestCode)
  180.         {
  181.             case PERMISSION_REQUEST_CODE:
  182.             {
  183.                 // If request is cancelled, the result arrays are empty.
  184.                 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  185.                 {
  186.                     startCamera();
  187.                 }
  188.                 else
  189.                 {
  190.                 }
  191.             }
  192.         }
  193.     }
  194.  
  195.     private static void setAppseeSessionKeepAlive(boolean appseeSessionKeepAlive)
  196.     {
  197.         MainActivity.appseeSessionKeepAlive = appseeSessionKeepAlive;
  198.  
  199.         if (appseeSessionKeepAlive)
  200.         {
  201.             handler.postDelayed(appseeForceFinishSession, MAX_APPSEE_SESSION_TIMEOUT);
  202.         }
  203.         else
  204.         {
  205.             handler.removeCallbacks(appseeForceFinishSession);
  206.         }
  207.     }
  208. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top