Advertisement
Guest User

Main2Activity

a guest
Aug 23rd, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.73 KB | None | 0 0
  1. package it.rapz.sfondideirapper;
  2.  
  3. import android.Manifest;
  4. import android.app.DownloadManager;
  5. import android.content.BroadcastReceiver;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.content.pm.PackageManager;
  11. import android.database.Cursor;
  12. import android.media.MediaScannerConnection;
  13. import android.net.Uri;
  14. import android.os.Environment;
  15. import android.provider.MediaStore;
  16. import android.support.v4.app.ActivityCompat;
  17. import android.support.v4.content.ContextCompat;
  18. import android.support.v7.app.AppCompatActivity;
  19. import android.os.Bundle;
  20. import android.util.Log;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.widget.AdapterView;
  24. import android.widget.ImageView;
  25. import android.widget.ListView;
  26. import android.widget.Toast;
  27.  
  28. import java.io.File;
  29. import java.util.Arrays;
  30. import java.util.Random;
  31.  
  32. public class Main2Activity extends AppCompatActivity {
  33.  
  34.     ListView listview;
  35.     Context context;
  36.     private static LayoutInflater inflater = null;
  37.     private BroadcastReceiver mDLCompleteReceiver;
  38.     private static BroadcastReceiver mDLCompleteReceiver2;
  39.     public static final int idFoto = 14;
  40.  
  41.     @Override
  42.     protected void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.activity_main2);
  45.  
  46.         Context context3 = getApplicationContext();
  47.         CharSequence text = "Premi uno sfondo per scaricarlo!";
  48.         int duration = Toast.LENGTH_LONG;
  49.  
  50.         Toast toast = Toast.makeText(context3, text, duration);
  51.         toast.show();
  52.  
  53.         int permissionCheck = ContextCompat.checkSelfPermission(Main2Activity.this,
  54.                 Manifest.permission.WRITE_EXTERNAL_STORAGE);
  55.         Log.e("ImageSaver", "WRITE_EXTERNAL_STORAGE:"+permissionCheck);
  56.  
  57.  
  58.         if (ContextCompat.checkSelfPermission(Main2Activity.this,
  59.                 Manifest.permission.WRITE_EXTERNAL_STORAGE)
  60.                 != PackageManager.PERMISSION_GRANTED) {
  61.  
  62.             if (ActivityCompat.shouldShowRequestPermissionRationale(Main2Activity.this,
  63.                     Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  64.  
  65.             } else {
  66.  
  67.                 // No explanation needed, we can request the permission.
  68.  
  69.                 ActivityCompat.requestPermissions(Main2Activity.this,
  70.                         new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
  71.                         1);
  72.  
  73.                 int permissionCheck2 = ContextCompat.checkSelfPermission(Main2Activity.this,
  74.                         Manifest.permission.WRITE_EXTERNAL_STORAGE);
  75.                 Log.e("ImageSaver", "WRITE_EXTERNAL_STORAGE:"+permissionCheck2);
  76.  
  77.             }
  78.         }
  79.  
  80.  
  81.         listview = (ListView) findViewById(R.id.listview);
  82.  
  83.  
  84.         String[] str = new String[idFoto];
  85.  
  86.         listview.setAdapter(new yourAdapter(this, str));
  87.  
  88.         listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  89.             @Override
  90.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  91.  
  92.                 Log.d("TAAAAG", "destra: ");
  93.                 Log.d("TAAAAG", "pos:"+position);
  94.                 Boolean ok = true;
  95.  
  96.                 ImageView mImageView = (ImageView) findViewById(R.id.immagine);
  97.  
  98.                 String name = "";
  99.                 if (name.isEmpty()) name = "sfondo_" + Math.abs(new Random().nextInt());
  100.                 int numFoto = idFoto - position;
  101.                 String url = "http://rapz.it/Sfondi/"+(numFoto)+".jpg";
  102.                 Log.d("NuovoTag", "pos:"+position);
  103.                 Log.d("NuovoTag", "id:"+idFoto);
  104.                 Log.d("NuovoTag", "http://rapz.it/Sfondi/"+(numFoto)+".jpg");
  105.  
  106.                 DownloadManager.Request request;
  107.  
  108.                 try {
  109.                     request = new DownloadManager.Request(Uri.parse(url));
  110.                 } catch (IllegalArgumentException e) {
  111.                     Log.d("NuovoTag", "Error: " + e.getMessage());
  112.                     request = null;
  113.                     ok = false;
  114.                 }
  115.                 /* allow mobile and WiFi downloads */
  116.                 if(ok) {
  117.                     request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
  118.                     request.setTitle("DM Example");
  119.                     request.setDescription("Downloading file");
  120.  
  121.                 /* we let the user see the download in a notification */
  122.                     request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
  123.                 /* Try to determine the file extension from the url. Only allow image types. You
  124.                  * can skip this check if you only plan to handle the downloaded file manually and
  125.                  * don't care about file managers not recognizing the file as a known type */
  126.                     String[] allowedTypes = {"png", "jpg", "jpeg", "gif", "webp"};
  127.                     String suffix = url.substring(url.lastIndexOf(".") + 1).toLowerCase();
  128.                     if (!Arrays.asList(allowedTypes).contains(suffix)) {
  129.                         Log.d("NuovoTag", "Invalid file extension. Allowed types: \n");
  130.                         String tvStatus = "";
  131.                         ok = false;
  132.                     }
  133.  
  134.                     if (ok) {
  135.                         /* set the destination path for this download */
  136.                         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS +
  137.                                 File.separator + "sfondi_dei_rapper", name + "." + suffix);
  138.                         /* allow the MediaScanner to scan the downloaded file */
  139.                         request.allowScanningByMediaScanner();
  140.                         final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  141.  
  142.                         /* this is our unique download id */
  143.                         final long DL_ID = dm.enqueue(request);
  144.  
  145.                         /* get notified when the download is complete */
  146.                         mDLCompleteReceiver = new BroadcastReceiver() {
  147.  
  148.                             @Override
  149.                             public void onReceive(Context context, Intent intent) {
  150.                                 /* our download */
  151.                                 if (DL_ID == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)) {
  152.  
  153.                                     //tvStatus.clearAnimation();
  154.                                     /* get the path of the downloaded file */
  155.                                     DownloadManager.Query query = new DownloadManager.Query();
  156.                                     query.setFilterById(DL_ID);
  157.                                     Cursor cursor = dm.query(query);
  158.                                     if (!cursor.moveToFirst()) {
  159.                                         //tvStatus.setText("Download error: cursor is empty");
  160.                                         Log.d("NuovoTag", "IDownload error: cursor is empty");
  161.                                         return;
  162.                                     }
  163.  
  164.                                     if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
  165.                                             != DownloadManager.STATUS_SUCCESSFUL) {
  166.                                         //tvStatus.setText("Download failed: no success status");
  167.                                         Log.d("NuovoTag", "Download failed: no success status");
  168.                                         return;
  169.                                     }
  170.  
  171.                                     String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  172.                                     Log.d("NuovoTag", "File download complete. Location: \n" + path);
  173.                                     Context context2 = getApplicationContext();
  174.                                     CharSequence text = "Sfondo scaricato! \nPuoi trovarlo nella galleria";
  175.                                     int duration = Toast.LENGTH_LONG;
  176.  
  177.                                     Toast toast = Toast.makeText(context2, text, duration);
  178.                                     toast.show();
  179.  
  180.                                 }
  181.                             }
  182.                         };
  183.                         /* register receiver to listen for ACTION_DOWNLOAD_COMPLETE action */
  184.                         registerReceiver(mDLCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  185.                     }
  186.                 }
  187.  
  188.  
  189.  
  190.             }
  191.         });
  192.  
  193.  
  194.     }
  195.  
  196.     public void MetodoDownload(int position, int destra)
  197.     {
  198.         Log.d("nTAAAAG", "1");
  199.         Log.d("TAAAAG", "destra: "+destra);
  200.         Log.d("TAAAAG", "pos:"+position);
  201.         Boolean ok = true;
  202.  
  203.  
  204.         String name = "";
  205.         if (name.isEmpty()) name = "sfondo_" + Math.abs(new Random().nextInt());
  206.         int numFoto = idFoto - position;
  207.         String url = "";
  208.         if(destra == 1)
  209.         {
  210.             url = "http://rapz.it/Sfondi/"+(numFoto*2)+".jpg";
  211.         }
  212.         else
  213.         {
  214.             url = "http://rapz.it/Sfondi/"+(numFoto)+".jpg";
  215.         }
  216.         Log.d("nTAAAAG", "1");
  217.         //////////////////
  218.         Log.d("NuovoTag", "pos:"+position);
  219.         Log.d("NuovoTag", "id:"+idFoto);
  220.         Log.d("NuovoTag", "http://rapz.it/Sfondi/"+(numFoto)+".jpg");
  221.  
  222.         DownloadManager.Request request;
  223.  
  224.         try {
  225.             request = new DownloadManager.Request(Uri.parse(url));
  226.         } catch (IllegalArgumentException e) {
  227.             Log.d("NuovoTag", "Error: " + e.getMessage());
  228.             request = null;
  229.             ok = false;
  230.         }
  231.                 /* allow mobile and WiFi downloads */
  232.         if(ok) {
  233.             request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
  234.             request.setTitle("DM Example");
  235.             request.setDescription("Downloading file");
  236.  
  237.                 /* we let the user see the download in a notification */
  238.             request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
  239.                 /* Try to determine the file extension from the url. Only allow image types. You
  240.                  * can skip this check if you only plan to handle the downloaded file manually and
  241.                  * don't care about file managers not recognizing the file as a known type */
  242.             String[] allowedTypes = {"png", "jpg", "jpeg", "gif", "webp"};
  243.             String suffix = url.substring(url.lastIndexOf(".") + 1).toLowerCase();
  244.             if (!Arrays.asList(allowedTypes).contains(suffix)) {
  245.                 Log.d("NuovoTag", "Invalid file extension. Allowed types: \n");
  246.                 String tvStatus = "";
  247.                 ok = false;
  248.             }
  249.  
  250.             if (ok) {
  251.                         /* set the destination path for this download */
  252.                 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS +
  253.                         File.separator + "sfondi_dei_rapper", name + "." + suffix);
  254.                         /* allow the MediaScanner to scan the downloaded file */
  255.                 request.allowScanningByMediaScanner();
  256.                 final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  257.  
  258.                         /* this is our unique download id */
  259.                 final long DL_ID = dm.enqueue(request);
  260.  
  261.                         /* get notified when the download is complete */
  262.                 mDLCompleteReceiver = new BroadcastReceiver() {
  263.  
  264.                     @Override
  265.                     public void onReceive(Context context, Intent intent) {
  266.                                 /* our download */
  267.                         if (DL_ID == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)) {
  268.  
  269.                             //tvStatus.clearAnimation();
  270.                                     /* get the path of the downloaded file */
  271.                             DownloadManager.Query query = new DownloadManager.Query();
  272.                             query.setFilterById(DL_ID);
  273.                             Cursor cursor = dm.query(query);
  274.                             if (!cursor.moveToFirst()) {
  275.                                 //tvStatus.setText("Download error: cursor is empty");
  276.                                 Log.d("NuovoTag", "IDownload error: cursor is empty");
  277.                                 return;
  278.                             }
  279.  
  280.                             if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
  281.                                     != DownloadManager.STATUS_SUCCESSFUL) {
  282.                                 //tvStatus.setText("Download failed: no success status");
  283.                                 Log.d("NuovoTag", "Download failed: no success status");
  284.                                 return;
  285.                             }
  286.  
  287.                             String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  288.                             Log.d("NuovoTag", "File download complete. Location: \n" + path);
  289.                             Context context2 = getApplicationContext();
  290.                             CharSequence text = "Sfondo scaricato! \nPuoi trovarlo nella galleria";
  291.                             int duration = Toast.LENGTH_LONG;
  292.  
  293.                             Toast toast = Toast.makeText(context2, text, duration);
  294.                             toast.show();
  295.  
  296.                         }
  297.                     }
  298.                 };
  299.                         /* register receiver to listen for ACTION_DOWNLOAD_COMPLETE action */
  300.                 registerReceiver(mDLCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  301.             }
  302.         }
  303.  
  304.         destra = 0;
  305.  
  306.  
  307.     }
  308.  
  309.     static public boolean resetExternalStorageMedia(Context context) {
  310.         if (Environment.isExternalStorageEmulated())
  311.             return (false);
  312.         Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
  313.         Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, uri);
  314.  
  315.         context.sendBroadcast(intent);
  316.         return (true);
  317.     }
  318.  
  319.     static public void notifyMediaScannerService(Context context, String path) {
  320.         MediaScannerConnection.scanFile(context,
  321.                 new String[] { path }, null,
  322.                 new MediaScannerConnection.OnScanCompletedListener() {
  323.                     public void onScanCompleted(String path, Uri uri) {
  324.                         Log.i("ExternalStorage", "Scanned " + path + ":");
  325.                         Log.i("ExternalStorage", "-> uri=" + uri);
  326.                     }
  327.                 });
  328.     }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement