Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.71 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package jvpg.cgodin.qc.ca.projetpldl.Private;
  17.  
  18. import android.app.ProgressDialog;
  19. import android.app.SearchManager;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.graphics.Bitmap;
  23. import android.graphics.BitmapFactory;
  24. import android.os.AsyncTask;
  25. import android.os.Bundle;
  26. import android.support.v7.app.AppCompatActivity;
  27. import android.support.v7.widget.LinearLayoutManager;
  28. import android.support.v7.widget.RecyclerView;
  29. import android.util.Base64;
  30. import android.util.Log;
  31. import android.view.LayoutInflater;
  32. import android.view.Menu;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import android.widget.Button;
  36. import android.widget.ImageView;
  37. import android.widget.SearchView;
  38. import android.widget.TextView;
  39. import android.widget.Toast;
  40.  
  41. import org.json.JSONArray;
  42. import org.json.JSONException;
  43. import org.json.JSONObject;
  44.  
  45. import java.io.BufferedInputStream;
  46. import java.io.BufferedReader;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.InputStream;
  49. import java.io.InputStreamReader;
  50. import java.math.BigInteger;
  51. import java.net.HttpURLConnection;
  52. import java.net.URL;
  53. import java.security.MessageDigest;
  54. import java.security.NoSuchAlgorithmException;
  55. import java.util.ArrayList;
  56. import java.util.LinkedList;
  57. import java.util.List;
  58.  
  59. import jvpg.cgodin.qc.ca.projetpldl.R;
  60. import jvpg.cgodin.qc.ca.projetpldl.entities.SearchBarItem;
  61.  
  62.  
  63. /**
  64. * Simple one-activity app that takes a search term via the Action Bar
  65. * and uses it as a query to search the contacts database via the Contactables
  66. * table.
  67. */
  68. public class SearchYTActivity extends AppCompatActivity {
  69.  
  70. List<SearchBarItem> lstSearchBarItem = new ArrayList<SearchBarItem>();
  71. String url = "http://424v.cgodin.qc.ca:8086/ProjetPLDL/webresources/musiques/creerMusique";
  72. //String youtubeUrl ="https://www.googleapis.com/youtube/v3/search AIzaSyCWK6nOn8VzfC5B51wwkv3ZXqLsl8gVTzc ";
  73. String youtubeKey = "AIzaSyCWK6nOn8VzfC5B51wwkv3ZXqLsl8gVTzc";
  74. String youtubeUrl = "https://www.googleapis.com/youtube/v3/search?";
  75. //part=snippet&q=eminem&type=video&key=<key>
  76. String imgUrl;
  77. String fluxJSON = "";
  78. String motRechercher;
  79.  
  80. Context context ;
  81. private RecyclerView recyclerView;
  82. private RecyclerView.Adapter adapter;
  83. private RecyclerView.LayoutManager layoutManager;
  84. private Bitmap bitmap;
  85. private List<String> mURLs = new LinkedList<String>();
  86. int compteurTempo=0;
  87.  
  88.  
  89. @Override
  90. protected void onCreate(Bundle savedInstanceState) {
  91. super.onCreate(savedInstanceState);
  92. setContentView(R.layout.activity_searchbar_list);
  93.  
  94. context=this;
  95.  
  96. }
  97.  
  98. @Override
  99. public boolean onCreateOptionsMenu(Menu menu) {
  100. // Inflate the menu; this adds items to the action bar if it is present.
  101. getMenuInflater().inflate(R.menu.searchbar, menu);
  102. Toast.makeText(SearchYTActivity.this, "wtf", Toast.LENGTH_SHORT).show();
  103.  
  104. // Associate searchable configuration with the SearchView
  105. SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
  106. SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
  107.  
  108. searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  109. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  110. @Override
  111. public boolean onQueryTextSubmit(String query) {
  112. Toast.makeText(SearchYTActivity.this, query, Toast.LENGTH_SHORT).show();
  113. motRechercher = query;
  114. new GGDownloadTask().execute("test");
  115.  
  116.  
  117. recyclerView = (RecyclerView) findViewById(R.id.searchbar_list);
  118. layoutManager = new LinearLayoutManager(context);
  119. recyclerView.setLayoutManager(layoutManager);
  120.  
  121. adapter = new PlanetAdapter(lstSearchBarItem,getApplicationContext());
  122. recyclerView.setAdapter(adapter);
  123.  
  124. return true;
  125. }
  126. @Override
  127. public boolean onQueryTextChange(String newText) {
  128. //Toast.makeText(MainActivity2.this, newText, Toast.LENGTH_SHORT).show();
  129. return false;
  130. }
  131. }
  132. );
  133. return true;
  134. }
  135.  
  136.  
  137. public void fillList(){
  138.  
  139. lstSearchBarItem = new ArrayList<SearchBarItem>();
  140.  
  141. JSONObject responseJSON= null;
  142. try {
  143. responseJSON = new JSONObject(fluxJSON);
  144.  
  145. JSONArray items = responseJSON.getJSONArray("items");
  146.  
  147.  
  148. for(int i=0;i<items.length();i++){
  149.  
  150. SearchBarItem s = new SearchBarItem();
  151. //s.setMusique("https://www.youtube.com/watch?v="+items.getJSONObject(i).getJSONObject("id").getString("videoId"));
  152. //OUUUUUUUUU
  153. s.setMusique("http://www.youtube.com/embed/"+items.getJSONObject(i).getJSONObject("id").getString("videoId"));
  154. s.setTitre(items.getJSONObject(i).getJSONObject("snippet").get("title")+"");
  155. s.setVignette("null");
  156. //Toast.makeText(MainActivity2.this, "Item "+i+" : ", Toast.LENGTH_SHORT).show();
  157. // Toast.makeText(MainActivity2.this, "Title : "+items.getJSONObject(i).getJSONObject("snippet").get("title"), Toast.LENGTH_SHORT).show();
  158. // Toast.makeText(MainActivity2.this, "Description : "+items.getJSONObject(i).getJSONObject("snippet").get("description"), Toast.LENGTH_SHORT).show();
  159. // Toast.makeText(MainActivity2.this, "thumbnails : "+items.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("default").getString("url"), Toast.LENGTH_SHORT).show();
  160. //Toast.makeText(MainActivity2.this, "URL : https://www.youtube.com/watch?v="+items.getJSONObject(i).getJSONObject("id").getString("videoId"), Toast.LENGTH_SHORT).show();
  161. mURLs.add(items.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("default").getString("url")+"");
  162. lstSearchBarItem.add(s);
  163.  
  164. }
  165. } catch (JSONException e) {
  166. e.printStackTrace();
  167. }
  168.  
  169. loadNext();
  170.  
  171. adapter.notifyDataSetChanged();
  172. recyclerView = (RecyclerView) findViewById(R.id.searchbar_list);
  173. layoutManager = new LinearLayoutManager(context);
  174. recyclerView.setLayoutManager(layoutManager);
  175.  
  176. adapter = new PlanetAdapter(lstSearchBarItem,getApplicationContext());
  177. recyclerView.setAdapter(adapter);
  178. }
  179. private void loadNext() {
  180.  
  181. if(mURLs.size()!=0)
  182. {
  183. String url = mURLs.remove(mURLs.size()-1);
  184. if (url != null) {
  185. new LoadImage().execute(url);
  186. }
  187. }
  188. }
  189.  
  190.  
  191. private class LoadImage extends AsyncTask<String, String, Bitmap> {
  192. @Override
  193. protected void onPreExecute() {
  194. super.onPreExecute();
  195. }
  196. protected Bitmap doInBackground(String... args) {
  197. try {
  198. bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. return bitmap;
  203. }
  204. protected void onPostExecute(Bitmap image) {
  205. if(image != null){
  206. //img.setImageBitmap(image);
  207.  
  208. SearchBarItem s = new SearchBarItem();
  209.  
  210. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  211. image.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
  212. byte[] b = baos.toByteArray();
  213. String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
  214. //Toast.makeText(MainActivity2.this, encodedImage, Toast.LENGTH_SHORT).show();
  215. lstSearchBarItem.get((lstSearchBarItem.size()-1)-compteurTempo).setVignette(encodedImage);
  216. compteurTempo++;
  217. if(compteurTempo==lstSearchBarItem.size())
  218. {
  219. compteurTempo=0;
  220. }
  221.  
  222. }else{
  223.  
  224. Toast.makeText(SearchYTActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
  225. }
  226. loadNext();
  227. }
  228. }
  229.  
  230.  
  231.  
  232.  
  233. public class GGDownloadTask extends AsyncTask<String, Integer, String> {
  234.  
  235. @Override
  236. protected void onPreExecute() {
  237. super.onPreExecute();
  238. }
  239.  
  240. //cette méthode prend en argument un tableau illimité de chaines de caractères
  241. @Override
  242. protected String doInBackground(String... params) {
  243. String resultString = null;
  244. resultString = getYoutubeJSON();
  245. fluxJSON = resultString;
  246. return resultString;
  247. }
  248.  
  249. @Override
  250. protected void onProgressUpdate(Integer... progress) {
  251. super.onProgressUpdate(progress);
  252. }
  253.  
  254. @Override
  255. protected void onPostExecute(String s) {
  256. super.onPostExecute(s);
  257. Log.i("JSON",fluxJSON);
  258. fillList();
  259. }
  260.  
  261. public String getYoutubeJSON() {
  262. HttpURLConnection c = null;
  263. String resultat = "";
  264. try {
  265.  
  266. URL u = new URL(youtubeUrl + "part=snippet&maxResults=20&q="+motRechercher+"&type=video&key="+youtubeKey);
  267. c = (HttpURLConnection) u.openConnection();
  268. c.setRequestMethod("GET");
  269. StringBuffer sb = new StringBuffer();
  270. InputStream is = null;
  271.  
  272. is = new BufferedInputStream(c.getInputStream());
  273. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  274. String line = "";
  275. while ((line = br.readLine()) != null){
  276. sb.append(line);
  277. }
  278. resultat = sb.toString();
  279. }
  280. catch(Exception e){
  281. Log.e("Read JSON Fail", Log.getStackTraceString(e));
  282. }
  283.  
  284. return resultat;
  285. }
  286.  
  287. }
  288.  
  289. public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> {
  290.  
  291. List<SearchBarItem> planetList;
  292.  
  293. public PlanetAdapter(List<SearchBarItem> planetList, Context context) {
  294. this.planetList = planetList;
  295. }
  296.  
  297. @Override
  298. public PlanetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  299. View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.searchbar_list_content,parent,false);
  300. PlanetViewHolder viewHolder=new PlanetViewHolder(v);
  301. return viewHolder;
  302. }
  303.  
  304. @Override
  305. public void onBindViewHolder(PlanetViewHolder holder, int position) {
  306.  
  307.  
  308. byte[] decodedString = Base64.decode(lstSearchBarItem.get(position).getVignette(), Base64.DEFAULT);
  309. Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
  310. holder.ivVignette.setImageBitmap(decodedByte);
  311.  
  312. holder.tvTitre.setText(lstSearchBarItem.get(position).getTitre());
  313.  
  314. holder.btnAjouter.setTag(lstSearchBarItem.get(position));
  315. holder.btnAjouter.setOnClickListener(new View.OnClickListener() {
  316. @Override
  317. public void onClick(View v) {
  318. Intent intent = new Intent(getApplicationContext(), AddMusiqueActivity.class);
  319. SearchBarItem s = (SearchBarItem)v.getTag();
  320. intent.putExtra("titre",s.getTitre());
  321. intent.putExtra("musique",s.getMusique());
  322. intent.putExtra("vignette",s.getVignette());
  323. startActivity(intent);
  324. }
  325. });
  326. }
  327.  
  328. @Override
  329. public int getItemCount() {
  330. return planetList.size();
  331. }
  332.  
  333. public class PlanetViewHolder extends RecyclerView.ViewHolder{
  334.  
  335. protected ImageView ivVignette;
  336. protected TextView tvTitre;
  337. protected Button btnAjouter;
  338.  
  339.  
  340. public PlanetViewHolder(View itemView) {
  341. super(itemView);
  342. ivVignette= (ImageView) itemView.findViewById(R.id.ivVignette);
  343. tvTitre= (TextView) itemView.findViewById(R.id.tvTitre);
  344. btnAjouter= (Button) itemView.findViewById(R.id.btnAjouter);
  345. }
  346. }
  347. }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement