Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.57 KB | None | 0 0
  1. package com.arteverywhere.francesco.art;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.ActivityNotFoundException;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.content.pm.PackageManager;
  10. import android.content.pm.ResolveInfo;
  11. import android.graphics.Bitmap;
  12. import android.graphics.drawable.BitmapDrawable;
  13. import android.graphics.drawable.Drawable;
  14. import android.net.ConnectivityManager;
  15. import android.net.NetworkInfo;
  16. import android.net.Uri;
  17. import android.os.AsyncTask;
  18. import android.os.Environment;
  19. import android.os.Handler;
  20. import android.provider.Settings;
  21. import android.support.v4.widget.SwipeRefreshLayout;
  22. import android.support.v7.app.ActionBarActivity;
  23. import android.os.Bundle;
  24. import android.text.Editable;
  25. import android.text.TextWatcher;
  26. import android.util.DisplayMetrics;
  27. import android.util.Log;
  28. import android.view.LayoutInflater;
  29. import android.view.Menu;
  30. import android.view.MenuItem;
  31. import android.view.View;
  32. import android.view.ViewGroup;
  33. import android.widget.AdapterView;
  34. import android.widget.ArrayAdapter;
  35. import android.widget.AutoCompleteTextView;
  36. import android.widget.BaseAdapter;
  37. import android.widget.Button;
  38. import android.widget.EditText;
  39. import android.widget.GridView;
  40. import android.widget.ImageView;
  41. import android.widget.ListView;
  42. import android.widget.TextView;
  43. import android.widget.Toast;
  44. import com.squareup.picasso.Picasso;
  45. import java.io.File;
  46. import java.io.FileOutputStream;
  47. import java.io.IOException;
  48. import java.util.ArrayList;
  49. import java.util.Iterator;
  50. import java.util.List;
  51.  
  52. import cod.com.appspot.art_everywhere_test.artEverywhere.ArtEverywhere;
  53. import cod.com.appspot.art_everywhere_test.artEverywhere.model.MainArtistBriefCollection;
  54. import cod.com.appspot.art_everywhere_test.artEverywhere.model.MainTechniqueResponseCollection;
  55.  
  56.  
  57.  
  58. public class MainActivity extends ActionBarActivity implements TaskCallbackDownloadArtworks, TaskCallbackRefreshArtworks, TaskCallbackDownloadFilteredArtworks, TaskCallbackDownloadArtistForGallery {
  59.  
  60. String[] urlPhoto = new String[AppConstants.numFoto];
  61. SharedPreferences pref;
  62. String email;
  63. boolean visitatore = false;
  64.  
  65. public static SwipeRefreshLayout swipeView;
  66. DatabaseArtwork db;
  67. GridView gridview;
  68.  
  69. AlertDialog dialog;
  70. String[] tecniche;
  71. String tecnicaScelta;
  72.  
  73. public DownloadArtworksByDate d;
  74. TaskCallbackDownloadArtworks callback;
  75. int pagina;
  76.  
  77. boolean isFiltri = false;
  78. boolean isSelected; //serve nel dialog per filtro città
  79. String selectedCity; // serve nel dialog per filtro città
  80. String[] artists;
  81. String[] artistsPic;
  82. String[] artistsMail;
  83. String selectedArtist; //email dell'artista selezionato
  84. String picArtista; //url della picture profile dell'artista selezionato
  85.  
  86. View fabUpload; //Floating action bar
  87.  
  88. // Filtered list of techniques
  89. private ArrayList<String> partialNames = new ArrayList<String>();
  90. // List of names matching criteria are listed here
  91. private ListView myList;
  92. // Field where user enters his search criteria
  93. private EditText nameCapture;
  94. // Adapter for myList
  95. private ArrayAdapter<String> myAdapter;
  96. TextView emptyText;
  97.  
  98. private CustomListArtists adapterArtists;
  99. private ArrayList<String> imagesurl = new ArrayList<String>();
  100. private ArrayList<String> artistsemails = new ArrayList<String>();
  101. private ArrayList<Artist> artistlist = new ArrayList<Artist>();
  102.  
  103.  
  104. @Override
  105. protected void onCreate(Bundle savedInstanceState) {
  106. super.onCreate(savedInstanceState);
  107. setContentView(R.layout.activity_main);
  108. //getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
  109.  
  110. pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
  111. SharedPreferences.Editor editor = pref.edit();
  112. email = pref.getString("email_artista",null);
  113.  
  114. /*if(getIntent().hasExtra("visitatore")){
  115. Bundle extras = getIntent().getExtras();
  116. visitatore = extras.getBoolean("visitatore",false);
  117. }*/
  118. if(email==null){
  119. visitatore=true;
  120. }
  121. if(!visitatore) { //visualizzo il floating button per l'upload solo se è artista
  122. fabUpload = findViewById(R.id.fab);
  123. fabUpload.setVisibility(View.VISIBLE);
  124. fabUpload.bringToFront();
  125. fabUpload.setOnClickListener(new View.OnClickListener() {
  126. @Override
  127. public void onClick(View v) {
  128. Intent myIntent = new Intent(MainActivity.this, UploadActivity.class);
  129. myIntent.putExtra("email", email);
  130. startActivity(myIntent);
  131. }
  132. });
  133. }
  134.  
  135.  
  136. /* VISUALIZZO ACTION BAR CON LOGO */
  137. getSupportActionBar().setHomeButtonEnabled(true);
  138. getSupportActionBar().setDisplayShowHomeEnabled(true);
  139. getSupportActionBar().setDisplayUseLogoEnabled(true);
  140. getSupportActionBar().setLogo(R.drawable.logo_trasparente);
  141. getSupportActionBar().setDisplayShowTitleEnabled(true);
  142.  
  143. swipeView = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
  144. swipeView.setEnabled(false);
  145.  
  146. gridview = (GridView) findViewById(R.id.gridView);
  147. gridview.setAdapter(new ImageAdapter(this));
  148.  
  149. db = new DatabaseArtwork(getApplicationContext());
  150. urlPhoto = db.getPhotos();
  151.  
  152. swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  153. @Override
  154. public void onRefresh() {
  155. swipeView.setRefreshing(true);
  156.  
  157. ( new Handler()).postDelayed(new Runnable() {
  158. @Override
  159. public void run() {
  160. swipeView.setRefreshing(false);
  161.  
  162. /* Devo chiamare l'API */
  163. chiamaApi();
  164. }
  165. }, 2000);
  166.  
  167. }
  168. });
  169.  
  170. callback=this; /* Serve per poter chiamare la funz di libr (con callback) all'interno dell'OnScrollListener */
  171. gridview.setOnScrollListener(new EndlessScrollListener() {
  172. @Override
  173. public void onLoadMore(int page, int totalItemsCount) {
  174.  
  175. // Triggered only when new data needs to be appended to the list
  176. pagina=page;
  177. //ho già le foto nel database. non devo ricaricarle
  178.  
  179. //Se filtro la ricerca "disabilito" l'endless scroll
  180. if(!isFiltri) {
  181. System.out.println("******"+urlPhoto.length+"******"+db.getAllArtworks().size());
  182. if(db.getAllArtworks().size()>=urlPhoto.length+4){
  183.  
  184. String [] b=db.getPhotosAfterScroll(((pagina/2)+1)*4+20);
  185. urlPhoto=new String[b.length];
  186. urlPhoto=b;
  187. gridview.setSelection(gridview.getFirstVisiblePosition());
  188. gridview.setAdapter(new ImageAdapter(getApplicationContext()));
  189.  
  190. }
  191. else{
  192.  
  193. String data=db.getArtworkFromUrl(urlPhoto[urlPhoto.length-1]).getData();
  194. new DownloadArtworksByDate(getApplicationContext(),db, callback, data).execute();
  195.  
  196. }
  197.  
  198. }
  199. }
  200.  
  201. });
  202.  
  203.  
  204.  
  205. gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  206. @Override
  207. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  208. Intent intent = new Intent(MainActivity.this, ArtworkDetails.class);
  209. String url = urlPhoto[position];
  210. intent.putExtra("photo",url);
  211.  
  212. startActivity(intent);
  213. }
  214. });
  215.  
  216. }
  217.  
  218.  
  219. public void chiamaApi(){
  220. if(checkNetwork()) new DownloadArtworksForRefresh(getApplicationContext(),db,this).execute();
  221. // Dopo aver completato l'asyntask viene chiamato done(boolean,boolean)
  222. }
  223.  
  224. public void done(boolean x, boolean a){
  225. /* I parametri x e a non servono a nulla */
  226.  
  227. /* Faccio una ricerca per data sul DB */
  228. urlPhoto = db.getArtworksOrderByDate();
  229.  
  230. /* Chiamo gridview.setAdapter(new ImageAdapter(this)); */
  231. gridview.setAdapter(new ImageAdapter(this));
  232. }
  233.  
  234. /* Serve per l'endless scroll */
  235. private String[] toArray(List<Artwork> l){
  236. int c=0;
  237. Iterator i =l.iterator();
  238. while(i.hasNext())
  239. c++;
  240. String [] f=new String[c];
  241. Iterator t=l.iterator();
  242. int g=0;
  243. while(t.hasNext()) {
  244. f[g] = l.get(g).getPhoto();
  245. g++;
  246. }
  247. return f;
  248. }
  249.  
  250. /* Serve per l'endless scroll - TaskCallback Download artworks */
  251. @Override
  252. public void done() {
  253. String [] a=db.getPhotosAfterScroll(((pagina/2)+1)*4+20);
  254. urlPhoto=new String[a.length];
  255. urlPhoto=a;
  256.  
  257. for(int i=0; i<urlPhoto.length; i++){
  258. if(urlPhoto[i]!=null){
  259. System.out.println(urlPhoto[i]);
  260.  
  261. }
  262. }
  263. gridview.setSelection(gridview.getFirstVisiblePosition());
  264. gridview.setAdapter(new ImageAdapter(this));
  265. }
  266.  
  267. //Custom adapter
  268. private class ImageAdapter extends BaseAdapter {
  269. private Context mContext;
  270.  
  271. public ImageAdapter(Context context) {
  272. mContext = context;
  273. }
  274.  
  275. @Override
  276. public int getCount() {
  277. return urlPhoto.length;
  278. }
  279.  
  280. @Override
  281. public Object getItem(int position) {
  282. return null;
  283. }
  284.  
  285. @Override
  286. public long getItemId(int position) {
  287. return 0;
  288. }
  289.  
  290. @Override
  291. public View getView(int position, View convertView,
  292. ViewGroup parent) {
  293.  
  294.  
  295. ImageView imageView;
  296. //check to see if we have a view
  297. if (convertView == null) {
  298. //no view - so create a new one
  299. imageView = new ImageView(mContext);
  300. } else {
  301. //use the recycled view object
  302. imageView = (ImageView) convertView;
  303. }
  304.  
  305. //Picasso.with(MainActivity.this).setDebugging(true);
  306.  
  307. DisplayMetrics metrics = new DisplayMetrics();
  308. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  309.  
  310. int size = metrics.widthPixels;
  311.  
  312. Picasso.with(MainActivity.this)
  313. .load("" + urlPhoto[position] + "")
  314. //.placeholder(R.raw.place_holder)
  315. .error(R.raw.place_holder)
  316. .noFade().resize(size/2, size/2)
  317. .centerCrop()
  318. .into(imageView);
  319. return imageView;
  320.  
  321. }
  322. }
  323.  
  324.  
  325. @Override
  326. public boolean onCreateOptionsMenu(Menu menu) {
  327. // Inflate the menu; this adds items to the action bar if it is present.
  328.  
  329. if(visitatore){
  330. //non deve visualizzare nulla nella action bar
  331. getMenuInflater().inflate(R.menu.menu_main_per_visitatori, menu);
  332. }else {
  333. getMenuInflater().inflate(R.menu.menu_main, menu);
  334. }
  335. return true;
  336. }
  337.  
  338. @Override
  339. public boolean onOptionsItemSelected(MenuItem item) {
  340. // Handle action bar item clicks here. The action bar will
  341. // automatically handle clicks on the Home/Up button, so long
  342. // as you specify a parent activity in AndroidManifest.xml.
  343. int id = item.getItemId();
  344.  
  345. if (id == R.id.logout) {
  346. //Login.signOutFromGplus();
  347. pref.edit().remove("email_artista").commit();
  348. pref.edit().putBoolean("token", true).commit();
  349.  
  350. Intent i = new Intent(MainActivity.this, Login.class);
  351. this.startActivity(i);
  352. Toast.makeText(getApplicationContext(), "Logout eseguito!", Toast.LENGTH_LONG).show();
  353. this.finish();
  354. return true;
  355. } else if (id == R.id.menu_filtro) {
  356. /* Faccio visualizzare l'alert dialog con la lista dei filtri possibli */
  357. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  358. final LayoutInflater inflater = getLayoutInflater();
  359. final View convertView = (View) inflater.inflate(R.layout.custom, null);
  360. alertDialog.setView(convertView);
  361. alertDialog.setTitle("Filtra ricerca");
  362. ListView lv = (ListView) convertView.findViewById(R.id.listView1);
  363.  
  364. final String[] filtri = {"Più recenti", "Tecnica", "Luogo", "Artista"};
  365.  
  366. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  367. @Override
  368. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  369. String item = filtri[position];
  370. System.out.println("Ho scelto: " + item);
  371. dialog.dismiss();
  372. //isTecnica = true;
  373. //effettuaDownload();
  374.  
  375. if (item.equalsIgnoreCase("Più recenti")) {
  376. seeMoreRecent();
  377. } else if (item.equalsIgnoreCase("Tecnica")) { //è stato selezionato -> TECNICA
  378. getTecniche();
  379. } else if (item.equalsIgnoreCase("Luogo")) { //è stato selezionato -> LUOGO
  380. seePlace();
  381. } else if (item.equalsIgnoreCase("Artista")) { //è stato selezionato -> ARTISTA
  382. seeArtists();
  383. }
  384. }
  385. });
  386. ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_black, filtri);
  387. lv.setAdapter(adapter);
  388. dialog = alertDialog.show();
  389. return true;
  390. } else if (id == R.id.menu_profile) {
  391. if(checkNetwork()) new DownloadArtistForGallery(getApplicationContext(), email, this).execute();
  392. } else if (id == R.id.menu_feedback) {
  393. Intent i = new Intent(Intent.ACTION_SEND);
  394. i.setType("message/rfc822");
  395. i.putExtra(Intent.EXTRA_EMAIL, new String[]{"arteverywhere00@gmail.com"});
  396. i.putExtra(Intent.EXTRA_SUBJECT, "Feedback - Art Everywhere");
  397. i.putExtra(Intent.EXTRA_TEXT, "");
  398. try {
  399. startActivity(Intent.createChooser(i, "Invia feedback..."));
  400. } catch (android.content.ActivityNotFoundException ex) {
  401. Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
  402. }
  403. return true;
  404. } else if (id == R.id.menu_facebook) {
  405. try {
  406. getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0);
  407. Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/1596309167253264"));
  408. startActivity(i);
  409. } catch (Exception e) {
  410. Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/ArtEverywhereApp"));
  411. startActivity(i);
  412. }
  413. } else if (id == R.id.menu_valutaci){
  414. Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
  415. Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
  416. try {
  417. startActivity(goToMarket);
  418. } catch (ActivityNotFoundException e) {
  419. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));
  420. }
  421. }
  422.  
  423. else if (id==R.id.menu_consiglia){
  424. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  425. final LayoutInflater inflater = getLayoutInflater();
  426. final View convertView = (View) inflater.inflate(R.layout.custom, null);
  427. alertDialog.setView(convertView);
  428. alertDialog.setTitle("Consiglia Art Everywhere ai tuoi amici");
  429.  
  430. final String[] filtri = { "Twitter", "Whatsapp", "Email"};
  431. final int[] immagini = {R.drawable.tw, R.drawable.wa, R.drawable.em};
  432.  
  433. CustomListInt adapter = new CustomListInt(MainActivity.this, filtri, immagini);
  434. ListView lv = (ListView) convertView.findViewById(R.id.listView1);
  435.  
  436. lv.setAdapter(adapter);
  437. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  438. @Override
  439. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  440. String item = filtri[position];
  441. dialog.dismiss();
  442.  
  443. if (item.equalsIgnoreCase("Whatsapp")) {
  444. eseguiShareWhatsapp();
  445. } else if (item.equalsIgnoreCase("Twitter")) {
  446. shareTwitter();
  447. } else if (item.equalsIgnoreCase("Email")) {
  448. shareEmail();
  449. }
  450. }
  451. });
  452.  
  453. dialog = alertDialog.show();
  454. return true;
  455. }
  456.  
  457.  
  458. else if(id==R.id.artista){
  459. pref.edit().putBoolean("token",true).commit();
  460. Intent myIntent = new Intent(MainActivity.this, Login.class);
  461. this.startActivity(myIntent);
  462. this.finish();
  463. }
  464.  
  465. return super.onOptionsItemSelected(item);
  466. }
  467.  
  468. public void seeMoreRecent(){
  469. getSupportActionBar().setDisplayShowTitleEnabled(true);
  470. getSupportActionBar().setTitle("Galleria");
  471. urlPhoto = db.getPhotos();
  472. gridview.setAdapter(new ImageAdapter(this));
  473. }
  474.  
  475. /* Scarico le tecniche da visualizzare nel dialog */
  476. private void getTecniche(){
  477. AsyncTask<Void, Void, MainTechniqueResponseCollection> getTec = new AsyncTask<Void, Void, MainTechniqueResponseCollection>() {
  478.  
  479. @Override
  480. protected MainTechniqueResponseCollection doInBackground(Void... unused) {
  481. // Retrieve service handle.
  482. ArtEverywhere apiServiceHandle = AppConstants.getApiServiceHandle(null);
  483.  
  484. try {
  485. MainTechniqueResponseCollection greeting = new MainTechniqueResponseCollection();
  486. ArtEverywhere.Techniques.Gettechniques get = apiServiceHandle.techniques().gettechniques();
  487. greeting = get.execute();
  488. return greeting;
  489. } catch (IOException e) {
  490. Toast.makeText(getApplicationContext(), "Exception during API call - tecniche!", Toast.LENGTH_LONG).show();
  491. //Log.d("ERRORE",e.getMessage());
  492. }
  493. return null;
  494. }
  495.  
  496. @Override
  497. protected void onPostExecute(MainTechniqueResponseCollection greeting) {
  498. if (greeting != null) {
  499. //System.out.println("SONO QUI");
  500. //System.out.println("tecniche: " + greeting.size());
  501. System.out.println("tecniche: " + greeting.getTechniques().size());
  502. tecniche = new String[greeting.getTechniques().size()];
  503. for(int i = 0; i < greeting.getTechniques().size(); i++){
  504. tecniche[i] = greeting.getTechniques().get(i).getTechnique();
  505. }
  506.  
  507. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  508. final LayoutInflater inflater = getLayoutInflater();
  509. //final View convertView = (View) inflater.inflate(R.layout.custom, null);
  510. final View convertView = (View) inflater.inflate(R.layout.custom_autocomplete_listview, null);
  511. alertDialog.setView(convertView);
  512. alertDialog.setTitle("Tecniche");
  513.  
  514. myList = (ListView) convertView.findViewById(R.id.listView1);
  515. nameCapture = (EditText) convertView.findViewById(R.id.name);
  516. nameCapture.setHint("Digita la tecnica che vuoi ricercare");
  517. emptyText = (TextView) convertView.findViewById(android.R.id.empty);
  518. myList.setEmptyView(emptyText);
  519.  
  520. emptyText.setOnClickListener(new View.OnClickListener() {
  521. @Override
  522. public void onClick(View v) {
  523. //Toast.makeText(getApplicationContext(), "A questo punto invio una mail", Toast.LENGTH_LONG).show();
  524. Intent i = new Intent(Intent.ACTION_SEND);
  525. i.setType("message/rfc822");
  526. i.putExtra(Intent.EXTRA_EMAIL , new String[]{"arteverywhere00@gmail.com"});
  527. i.putExtra(Intent.EXTRA_SUBJECT, "[Suggerimento] Tecnica Mancante");
  528. i.putExtra(Intent.EXTRA_TEXT , "Voglio suggerire la seguente tecnica mancante: " + nameCapture.getText().toString());
  529. try {
  530. startActivity(Intent.createChooser(i, "Invia suggerimento..."));
  531. } catch (android.content.ActivityNotFoundException ex) {
  532. Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
  533. }
  534. }
  535. });
  536.  
  537.  
  538.  
  539. myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  540. @Override
  541. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  542. //System.out.println(tecniche[position]);
  543. //tecnicaScelta = tecniche[position];
  544. tecnicaScelta = partialNames.get(position);
  545. System.out.println("Ho scelto: " + tecnicaScelta);
  546. dialog.dismiss();
  547. //isTecnica = true;
  548. System.out.println("avvio il download per tecnica");
  549. effettuaDownloadPerTecnica(tecnicaScelta);
  550. }
  551. });
  552.  
  553. for(int i = 0; i < tecniche.length;i++) partialNames.add(i, tecniche[i]);
  554.  
  555. //myAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_black, tecniche);
  556. myAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_black, partialNames);
  557. myList.setAdapter(myAdapter);
  558.  
  559. nameCapture.addTextChangedListener(new TextWatcher() {
  560.  
  561. // As the user types in the search field, the list is
  562. @Override
  563. public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  564. AlterAdapter();
  565. }
  566.  
  567. // Not used for this program
  568. @Override
  569. public void afterTextChanged(Editable arg0) {
  570.  
  571. }
  572.  
  573. // Not uses for this program
  574. @Override
  575. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  576. // TODO Auto-generated method stub
  577.  
  578. }
  579. });
  580.  
  581. dialog = alertDialog.show();
  582. //Toast.makeText(getApplicationContext(), "Upload successfull!", Toast.LENGTH_LONG).show();
  583. } else {
  584. Toast.makeText(getApplicationContext(), "No greetings were returned by the API.", Toast.LENGTH_LONG).show();
  585. }
  586. }
  587. };
  588. if(checkNetwork()) getTec.execute();
  589. }
  590.  
  591. private void AlterAdapter() {
  592. if (nameCapture.getText().toString().isEmpty()) {
  593. partialNames.clear();
  594. for(int i = 0; i < tecniche.length;i++) partialNames.add(i,tecniche[i]);
  595. myAdapter.notifyDataSetChanged();
  596. }
  597. else {
  598. partialNames.clear();
  599. for (int i = 0; i < tecniche.length; i++) {
  600. if (tecniche[i].toString().toUpperCase().contains(nameCapture.getText().toString().toUpperCase())) {
  601. partialNames.add(tecniche[i].toString());
  602. }
  603. myAdapter.notifyDataSetChanged();
  604. }
  605. }
  606.  
  607. if(myAdapter.getCount()==0){
  608. emptyText.setVisibility(View.VISIBLE);
  609. emptyText.setText("Clicca per segnalare tecnica mancante!");
  610. }
  611. }
  612.  
  613. private void AlterAdapterArtists() {
  614. if (nameCapture.getText().toString().isEmpty()) {
  615. partialNames.clear();
  616. imagesurl.clear();
  617. artistsemails.clear();
  618. artistlist.clear();
  619. for(int i = 0; i < artists.length;i++){
  620. //partialNames.add(i,artists[i]);
  621. //imagesurl.add(i,artistsPic[i]);
  622. partialNames.add(i, artists[i]);
  623. imagesurl.add(i, artistsPic[i]);
  624. artistsemails.add(i, artistsMail[i]);
  625. Artist a = new Artist(artists[i],artistsPic[i],artistsMail[i]);
  626. artistlist.add(a);
  627. }
  628. adapterArtists.notifyDataSetChanged();
  629. //adapterArtists = new CustomListArtists(MainActivity.this, partialNames, imagesurl);
  630. //myList.setAdapter(adapterArtists);
  631. }
  632. else {
  633. partialNames.clear();
  634. imagesurl.clear();
  635. artistsemails.clear();
  636. artistlist.clear();
  637. for (int i = 0; i < artists.length; i++) {
  638. if (artists[i].toString().toUpperCase().contains(nameCapture.getText().toString().toUpperCase())) {
  639. //partialNames.add(artists[i].toString());
  640. //imagesurl.add(artistsPic[i].toString());
  641. System.out.println(artists[i].toString());
  642. System.out.println(artistsPic[i].toString());
  643. partialNames.add(artists[i]);
  644. imagesurl.add(artistsPic[i]);
  645. artistsemails.add(artistsMail[i]);
  646. Artist a = new Artist(artists[i],artistsPic[i],artistsMail[i]);
  647. artistlist.add(a);
  648. }
  649. adapterArtists.notifyDataSetChanged();
  650. //adapterArtists = new CustomListArtists(MainActivity.this, partialNames, imagesurl);
  651. //myList.setAdapter(adapterArtists);
  652. }
  653. }
  654.  
  655. if(adapterArtists.getCount()==0){
  656. emptyText.setVisibility(View.VISIBLE);
  657. emptyText.setText("Nessun artista trovato!");
  658. }
  659. }
  660.  
  661. public void effettuaDownloadPerTecnica(String tecnicaScelta){
  662. if(checkNetwork()) new DownloadArtworksByTechinique(getApplicationContext(),db,this,tecnicaScelta).execute();
  663. }
  664.  
  665. public void seePlace(){
  666. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  667. final LayoutInflater inflater = getLayoutInflater();
  668. final View convertView = (View) inflater.inflate(R.layout.custom_filter_place, null);
  669. alertDialog.setView(convertView);
  670. alertDialog.setTitle("Luogo");
  671.  
  672. isSelected = false;
  673. AutoCompleteTextView place = (AutoCompleteTextView) convertView.findViewById(R.id.editText3);
  674. place.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
  675. place.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  676. @Override
  677. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  678. isSelected = true;
  679. String str = (String) parent.getItemAtPosition(position);
  680. selectedCity = str;
  681. Log.d("PLACE", str);
  682. }
  683. });
  684.  
  685. Button cerca = (Button) convertView.findViewById(R.id.buttonOk);
  686. cerca.setOnClickListener(new View.OnClickListener() {
  687. @Override
  688. public void onClick(View v) {
  689. if(!isSelected){
  690. Toast.makeText(getApplicationContext(), "Attenzione! Devi selezionare uno dei suggerimenti proposti", Toast.LENGTH_LONG).show();
  691. }else{
  692. //avvio la chiamata alla funzione di libreria
  693. dialog.dismiss();
  694. effettuaDownloadPerLuogo(selectedCity);
  695. }
  696. }
  697. });
  698.  
  699. dialog = alertDialog.show();
  700. }
  701.  
  702. public void effettuaDownloadPerLuogo(String city){
  703. if(checkNetwork()) new DownloadArtworksByPlace(getApplicationContext(),db,this,city).execute();
  704. }
  705.  
  706. public void seeArtists(){
  707. AsyncTask<Void, Void, MainArtistBriefCollection> getArtists = new AsyncTask<Void, Void, MainArtistBriefCollection>() {
  708.  
  709. @Override
  710. protected MainArtistBriefCollection doInBackground(Void... unused) {
  711. // Retrieve service handle.
  712. ArtEverywhere apiServiceHandle = AppConstants.getApiServiceHandle(null);
  713.  
  714. try {
  715. MainArtistBriefCollection greeting = new MainArtistBriefCollection();
  716. ArtEverywhere.Artistlist.Getartists get = apiServiceHandle.artistlist().getartists();
  717. greeting = get.execute();
  718. return greeting;
  719. } catch (IOException e) {
  720. Toast.makeText(getApplicationContext(), "Exception during API call - get Artists!", Toast.LENGTH_LONG).show();
  721. //Log.d("ERRORE",e.getMessage());
  722. }
  723. return null;
  724. }
  725.  
  726. @Override
  727. protected void onPostExecute(MainArtistBriefCollection greeting) {
  728. if (greeting != null) {
  729. artists = new String[greeting.getArtists().size()];
  730. artistsPic = new String[greeting.getArtists().size()];
  731. artistsMail = new String[greeting.getArtists().size()];
  732.  
  733. for(int i= 0;i < greeting.getArtists().size(); i++){
  734. artists[i] = greeting.getArtists().get(i).getCognome() + " " + greeting.getArtists().get(i).getNome();
  735. artistsPic[i] = greeting.getArtists().get(i).getPic();
  736. artistsMail[i] = greeting.getArtists().get(i).getEmail();
  737. }
  738.  
  739. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  740. final LayoutInflater inflater = getLayoutInflater();
  741. final View convertView = (View) inflater.inflate(R.layout.custom_autocomplete_listview, null);
  742. alertDialog.setView(convertView);
  743. alertDialog.setTitle("Artisti");
  744.  
  745.  
  746. for(int i = 0; i < artists.length;i++){
  747. partialNames.add(i, artists[i]);
  748. imagesurl.add(i, artistsPic[i]);
  749. artistsemails.add(i, artistsMail[i]);
  750. Artist a = new Artist(artists[i],artistsPic[i],artistsMail[i]);
  751. artistlist.add(a);
  752. }
  753.  
  754. //adapterArtists = new CustomListArtists(MainActivity.this, partialNames, imagesurl);
  755. adapterArtists = new CustomListArtists(MainActivity.this, artistlist);
  756.  
  757. myList = (ListView) convertView.findViewById(R.id.listView1);
  758. nameCapture = (EditText) convertView.findViewById(R.id.name);
  759. nameCapture.setHint("Digita l'artista che vuoi ricercare");
  760. emptyText = (TextView) convertView.findViewById(android.R.id.empty);
  761. myList.setEmptyView(emptyText);
  762. myList.setAdapter(adapterArtists);
  763.  
  764. nameCapture.addTextChangedListener(new TextWatcher() {
  765.  
  766. // As the user types in the search field, the list is
  767. @Override
  768. public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  769. AlterAdapterArtists();
  770. }
  771.  
  772. // Not used for this program
  773. @Override
  774. public void afterTextChanged(Editable arg0) {
  775.  
  776. }
  777.  
  778. // Not uses for this program
  779. @Override
  780. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  781. // TODO Auto-generated method stub
  782.  
  783. }
  784. });
  785.  
  786.  
  787. myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  788. @Override
  789. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  790. selectedArtist = artistlist.get(position).getEmail();
  791. picArtista = artistlist.get(position).getPhoto();
  792. dialog.dismiss();
  793. getArtistInfo();
  794.  
  795. }
  796. });
  797.  
  798.  
  799. dialog = alertDialog.show();
  800.  
  801. } else {
  802. Toast.makeText(getApplicationContext(), "No greetings were returned by the API.", Toast.LENGTH_LONG).show();
  803. }
  804. }
  805. };
  806. if(checkNetwork()) getArtists.execute();
  807. }
  808.  
  809. public void getArtistInfo(){
  810. if(checkNetwork()) new DownloadArtistForGallery(getApplicationContext(),selectedArtist,this).execute();
  811. }
  812.  
  813. @Override
  814. public void done(String email, String nc, String pic, String nick, String bio, String sito) {
  815. Intent intent = new Intent(MainActivity.this, ArtistProfile.class);
  816. intent.putExtra("nomecognome",nc);
  817. intent.putExtra("bio",bio);
  818. intent.putExtra("sito",sito);
  819. intent.putExtra("nickname",nick);
  820. intent.putExtra("email",email);
  821. intent.putExtra("pic", pic);
  822.  
  823. startActivity(intent);
  824. }
  825.  
  826. //metodo di ritorno dalle funzioni di libreria dei filtri
  827. public void done(int x){
  828. isFiltri = true;
  829. getSupportActionBar().setDisplayShowTitleEnabled(true);
  830.  
  831. if(x==0){ //callback from DownloadArtworksByTechnique
  832. urlPhoto = db.getArtworksFromTechinique(tecnicaScelta);
  833. getSupportActionBar().setTitle(tecnicaScelta);
  834.  
  835. }else if(x==1){ //callback from DownloadArtworksByPlace
  836. urlPhoto = db.getArtworksFromPlace(selectedCity);
  837. getSupportActionBar().setTitle(selectedCity);
  838.  
  839. }
  840. gridview.setSelection(gridview.getFirstVisiblePosition());
  841. gridview.setAdapter(new ImageAdapter(this));
  842.  
  843. }
  844.  
  845.  
  846. private void eseguiShareFacebook(){
  847. Intent intent = new Intent(Intent.ACTION_SEND);
  848. intent.setType("text/plain");
  849. intent.putExtra(android.content.Intent.EXTRA_TEXT, "https://bit.ly/AEDownload");
  850. intent.putExtra(android.content.Intent.EXTRA_STREAM, R.drawable.splash_logo);
  851. }
  852.  
  853. private void shareTwitter() {
  854. try {
  855. Uri uri = Uri.parse("android.resource://com.arteverywhere.francesco.art/drawable/logo");
  856.  
  857. Intent tweetIntent = new Intent(Intent.ACTION_SEND);
  858. tweetIntent.putExtra(Intent.EXTRA_TEXT, "Scarica Art Everywhere e scopri i nuovi artisti emergenti! L'app è completamente gratuita! http://bit.ly/AEDownload");
  859. tweetIntent.putExtra(Intent.EXTRA_STREAM, uri);
  860. tweetIntent.setType("image/jpeg");
  861. PackageManager pm = MainActivity.this.getPackageManager();
  862. List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
  863. boolean resolved = false;
  864. for (ResolveInfo ri : lract) {
  865. if (ri.activityInfo.name.contains("twitter")) {
  866. tweetIntent.setClassName(ri.activityInfo.packageName,
  867. ri.activityInfo.name);
  868. resolved = true;
  869. break;
  870. }
  871. }
  872. startActivity(resolved ?
  873. tweetIntent :
  874. Intent.createChooser(tweetIntent, "Choose one"));
  875. } catch (final ActivityNotFoundException e) {
  876. Toast.makeText(MainActivity.this, "Devi prima installare Twitter!", Toast.LENGTH_SHORT).show();
  877. }
  878. }
  879.  
  880. public void eseguiShareWhatsapp() {
  881. Uri uri = Uri.parse("android.resource://com.arteverywhere.francesco.art/drawable/logo");
  882. Intent shareIntent = new Intent();
  883. shareIntent.setPackage("com.whatsapp");
  884. shareIntent.setAction(Intent.ACTION_SEND);
  885. shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
  886. shareIntent.putExtra(Intent.EXTRA_TEXT, "Scarica Art Everywhere e scopri tutti gli artisti emergenti! Scarica ora l'app! " + "http://bit.ly/AEDownload");
  887. shareIntent.setType("image/*");
  888. // Launch sharing dialog for image
  889. startActivity(Intent.createChooser(shareIntent, "Share Image"));
  890. }
  891.  
  892.  
  893. public void shareEmail(){
  894. Uri uri = Uri.parse("android.resource://com.arteverywhere.francesco.art/drawable/logo");
  895.  
  896. Intent i = new Intent(Intent.ACTION_SEND);
  897. i.setType("message/rfc822");
  898. i.putExtra(Intent.EXTRA_SUBJECT, "Scarica Art Everywhere!");
  899. i.putExtra(Intent.EXTRA_TEXT, "Ciao! Scarica Art Everywhere e scopri tutti i nuovi artisti emergenti! Scarica ora l'app! CLICCA IL SEGUENTE LINK: http://bit.ly/AEDownload");
  900. i.putExtra(Intent.EXTRA_STREAM, uri);
  901. try {
  902. startActivity(Intent.createChooser(i, "Consiglia Art Everywhere.."));
  903. } catch (android.content.ActivityNotFoundException ex) {
  904. Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
  905. }
  906. }
  907.  
  908. public Uri getLocalBitmapUri(ImageView imageView) {
  909. // Extract Bitmap from ImageView drawable
  910. Drawable drawable = imageView.getDrawable();
  911. Bitmap bmp = null;
  912. if (drawable instanceof BitmapDrawable){
  913. bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
  914. } else {
  915. return null;
  916. }
  917. // Store image to default external storage directory
  918. Uri bmpUri = null;
  919. try {
  920. File file = new File(Environment.getExternalStoragePublicDirectory(
  921. Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
  922. file.getParentFile().mkdirs();
  923. FileOutputStream out = new FileOutputStream(file);
  924. bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
  925. out.close();
  926. bmpUri = Uri.fromFile(file);
  927. } catch (IOException e) {
  928. e.printStackTrace();
  929. }
  930. return bmpUri;
  931. }
  932.  
  933. public boolean checkNetwork() {
  934. ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  935. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  936. boolean isOnline = (netInfo != null && netInfo.isConnectedOrConnecting());
  937. if(isOnline) {
  938. return true;
  939. }else{
  940. new AlertDialog.Builder(this)
  941. .setTitle("Ops..qualcosa è andato storto!")
  942. .setMessage("Sembra che tu non sia collegato ad internet! ")
  943. .setPositiveButton("Impostazioni", new DialogInterface.OnClickListener() {
  944. public void onClick(DialogInterface dialog, int which) {
  945. // continue with delete
  946. Intent callGPSSettingIntent = new Intent(Settings.ACTION_SETTINGS);
  947. startActivityForResult(callGPSSettingIntent,0);
  948. }
  949. }).show();
  950. return false;
  951. }
  952. }
  953.  
  954.  
  955.  
  956.  
  957. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement