Java tutorial
/******************************************************************************* * Copyright 2011-2013 Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package sjizl.com; import static sjizl.com.CommonUtilities.DISPLAY_MESSAGE_ACTION; import static sjizl.com.CommonUtilities.options; import static sjizl.com.CommonUtilities.options_vierkant; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Locale; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import sjizl.com.LoginActivity; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.ClipData; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.support.v4.app.FragmentTransaction; import android.text.Editable; import android.text.Html; import android.text.Spanned; import android.text.Html.ImageGetter; import android.text.TextWatcher; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.ViewManager; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.Filter; import android.widget.Filterable; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import sjizl.com.Constants.Extra; import sjizl.com.libary.UserFunctions; import com.google.android.gcm.GCMRegistrar; import com.navdrawer.SimpleSideDrawer; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; /** * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class ChatFriendListFragment extends AbsListViewBaseActivityFragment implements AsyncDelegate { SimpleSideDrawer slide_me; boolean clicked = false; ProgressBar progressBar_hole; int PositionSelected, position222; ImageView Image3, imageView1_logo, imageView_bericht; EditText editText_search; EditText search; LinearLayout right_lin, left_lin1, middle_lin, left_lin2, left_lin3, left_lin4; String[] imageUrls, imageUrls2, names, laatste_bericht, fotos, fotos_nums, aantal_new_ber, laatste_tijd, is_onlines, gelezens, pids; String[] stockArr0, stockArr, stockArr2, stockArr3, stockArr4, stockArr5, stockArr6, stockArr7, stockArr8, stockArr9; String username, password, naam, foto, foto_num; private static final String ARG_POSITION = "position"; int i22d; ArrayList<String> stock_list, stock_list0, stock_list2, stock_list3, stock_list4, stock_list5, stock_list6, stock_list7, stock_list8, stock_list9; String naam_to_delete, del_con_result; AlertDialogManager alert = new AlertDialogManager(); UserFunctions userFunctions = new UserFunctions(); ItemAdapter adapter = new ItemAdapter(null, null); ItemAdapter adapter2 = new ItemAdapter(null, null); int mSelectedItem; public static String getFriendsURL = "http://m.sjizl.com/new_version/ajax/get_friends.php"; public static ChatFriendListFragment newInstance(int position) { ChatFriendListFragment f = new ChatFriendListFragment(); Bundle b = new Bundle(); b.putInt(ARG_POSITION, position); f.setArguments(b); return f; } @Override public void onDestroy() { try { getActivity().unregisterReceiver(mHandleMessageReceiver2); GCMRegistrar.onDestroy(getActivity()); } catch (Exception e) { Log.e("UnRegister Receiver Error", "> " + e.getMessage()); } super.onDestroy(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater factory = LayoutInflater.from(getActivity()); View myView = factory.inflate(R.layout.activity_chatfriendlist_fragment, null); position222 = getArguments().getInt(ARG_POSITION); i22d = this.getId(); AbsListViewBaseActivity.imageLoader .init(ImageLoaderConfiguration.createDefault(getActivity().getBaseContext())); if (userFunctions.isUserLoggedIn(getActivity().getApplicationContext())) { Image3 = (ImageView) getActivity().findViewById(R.id.imageView2_dashboard); imageView1_logo = (ImageView) getActivity().findViewById(R.id.imageView1_logo); imageView_bericht = (ImageView) getActivity().findViewById(R.id.imageView_bericht); right_lin = (LinearLayout) myView.findViewById(R.id.search_right_lin); left_lin1 = (LinearLayout) getActivity().findViewById(R.id.left_lin1); left_lin3 = (LinearLayout) getActivity().findViewById(R.id.left_lin3); left_lin4 = (LinearLayout) getActivity().findViewById(R.id.left_lin4); middle_lin = (LinearLayout) myView.findViewById(R.id.search_middle_lin); progressBar_hole = (ProgressBar) getActivity().findViewById(R.id.progressBar_hole); progressBar_hole.setVisibility(View.INVISIBLE); final ImageView left_button; left_button = (ImageView) getActivity().findViewById(R.id.imageView1_back); editText_search = new EditText(getActivity().getApplicationContext()); SharedPreferences sp = getActivity().getApplicationContext().getSharedPreferences("loginSaved", Context.MODE_PRIVATE); naam = sp.getString("naam", null); username = sp.getString("username", null); password = sp.getString("password", null); foto = sp.getString("foto", null); foto_num = sp.getString("foto_num", null); getActivity().registerReceiver(mHandleMessageReceiver2, new IntentFilter(DISPLAY_MESSAGE_ACTION)); imageLoader.displayImage("http://sjizl.com/fotos/" + foto_num + "/thumbs/" + foto, Image3, options); listView = (ListView) myView.findViewById(android.R.id.list); //imageLoader.displayImage("drawable://" + R.drawable.browse, imageView_bericht, options); listView.setLongClickable(true); registerForContextMenu(listView); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //startImagePagerActivity(position); String selectedValue0 = pids[position]; String selectedValue = names[position]; String selectedValue2 = laatste_bericht[position]; String selectedValue3 = imageUrls2[position]; String selectedValue4 = fotos[position]; String selectedValue5 = fotos_nums[position]; Intent dashboard = new Intent(getActivity().getApplicationContext(), ChatActivity.class); dashboard.putExtra("pid_user", selectedValue0); dashboard.putExtra("user", selectedValue); dashboard.putExtra("user_foto", selectedValue4); dashboard.putExtra("user_foto_num", selectedValue5); startActivity(dashboard); } }); right_lin.setOnTouchListener(new OnTouchListener() { @SuppressLint("DefaultLocale") @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_POINTER_DOWN) { //textView1_bgtext.setVisibility(View.GONE); //textView2_under_title.setVisibility(View.GONE); //editText_search.setId(9000); if (!clicked) { middle_lin.addView(editText_search); editText_search.requestFocus(); clicked = true; } else { //clicked=false; } editText_search.addTextChangedListener(new TextWatcher() { @SuppressLint("DefaultLocale") @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { ArrayList<String> new_stock_list0 = new ArrayList<String>(); ArrayList<String> new_stock_list = new ArrayList<String>(); ArrayList<String> new_stock_list2 = new ArrayList<String>(); ArrayList<String> new_stock_list3 = new ArrayList<String>(); ArrayList<String> new_stock_list4 = new ArrayList<String>(); ArrayList<String> new_stock_list5 = new ArrayList<String>(); ArrayList<String> new_stock_list6 = new ArrayList<String>(); ArrayList<String> new_stock_list7 = new ArrayList<String>(); ArrayList<String> new_stock_list8 = new ArrayList<String>(); int p = stock_list2.size(); for (int i = 0; i < p; i++) { // **line 2** Log.d("" + i, "" + stock_list2.get(i)); if (stock_list2.get(i).toString().trim().toLowerCase() .contains(cs.toString().toLowerCase())) { Log.d("" + i, "" + stock_list2.get(i)); new_stock_list8.add(stock_list8.get(i)); new_stock_list7.add(stock_list7.get(i)); new_stock_list6.add(stock_list6.get(i)); new_stock_list4.add(stock_list4.get(i)); new_stock_list5.add(stock_list5.get(i)); new_stock_list2.add(stock_list2.get(i)); new_stock_list3.add(stock_list3.get(i)); new_stock_list.add(stock_list.get(i)); new_stock_list0.add(stock_list0.get(i)); } } stockArr0 = new String[new_stock_list0.size()]; stockArr = new String[new_stock_list.size()]; stockArr2 = new String[new_stock_list2.size()]; stockArr3 = new String[new_stock_list3.size()]; stockArr4 = new String[new_stock_list4.size()]; stockArr5 = new String[new_stock_list5.size()]; stockArr6 = new String[new_stock_list6.size()]; stockArr7 = new String[new_stock_list7.size()]; stockArr8 = new String[new_stock_list8.size()]; pids = new_stock_list0.toArray(stockArr0); is_onlines = new_stock_list8.toArray(stockArr8); laatste_tijd = new_stock_list7.toArray(stockArr7); aantal_new_ber = new_stock_list6.toArray(stockArr6); imageUrls2 = new_stock_list.toArray(stockArr); names = new_stock_list2.toArray(stockArr2); laatste_bericht = new_stock_list3.toArray(stockArr3); fotos = new_stock_list4.toArray(stockArr4); fotos_nums = new_stock_list5.toArray(stockArr5); adapter.notifyDataSetChanged(); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub // Toast.makeText(getApplicationContext(), "search for"+arg0.toString(), Toast.LENGTH_SHORT).show(); } }); //right_lin.setBackgroundColor(Color.DKGRAY); //v.setBackgroundColor(Color.DKGRAY); //Intent dash = new Intent(getApplicationContext(),ChatFriendList.class); //dash.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //startActivity(dash); } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { //right_lin.setBackgroundColor(Color.BLACK); //v.setBackgroundColor(Color.BLACK); } return false; } }); new UpdateList().execute(); } else { // user is not logged in show login screen Intent login = new Intent(getActivity().getApplicationContext(), LoginActivity.class); login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(login); // Closing dashboard screen getActivity().finish(); } return myView; } public void asyncComplete(boolean success) { adapter.notifyDataSetChanged(); Log.i("list_updated", "list_updated"); Log.e("list_updated", "list_updated"); } class ItemAdapter extends BaseAdapter { String name; private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); private List<String> originalData = null; private List<String> filteredData = null; public ItemAdapter(Context context, List<String> data) { this.filteredData = data; this.originalData = data; } @Override public int getCount() { return imageUrls2.length; } public void addItem() { this.name = "aaa"; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { convertView = getActivity().getLayoutInflater().inflate(R.layout.item_list_image, null); TextView text = (TextView) convertView.findViewById(R.id.text); TextView text2 = (TextView) convertView.findViewById(R.id.text2); ImageView image = (ImageView) convertView.findViewById(R.id.image); ImageView image_online = (ImageView) convertView.findViewById(R.id.image_online); TextView text_new = (TextView) convertView.findViewById(R.id.text_new); TextView time = (TextView) convertView.findViewById(R.id.time); ImageView image_photo = (ImageView) convertView.findViewById(R.id.image_photo); ImageView chat_right_new1 = (ImageView) convertView.findViewById(R.id.chat_right_new1); ImageView chat_right_new2 = (ImageView) convertView.findViewById(R.id.chat_right_new2); text_new.setText(aantal_new_ber[position]); if (Integer.parseInt(aantal_new_ber[position].toString()) > 0) { text_new.bringToFront(); text_new.setBackgroundResource(R.drawable.rounded_corners); text_new.requestLayout(); text_new.setText(aantal_new_ber[position].toString()); } else { text_new.setVisibility(View.GONE); } if (is_onlines[position].equals("true")) { image_online.bringToFront(); } else { image_online.setVisibility(View.GONE); } time.setText(laatste_tijd[position]); text.setText(names[position]); imageLoader.displayImage(imageUrls2[position], image_photo, options, animateFirstListener); image_photo.invalidate(); String str = laatste_bericht[position].replace("src=/i/smiles2/", "src=http://www.sjizl.com/i/smiles2/"); String stringWithHtml = str; SmilesViewUtils.setText(text2, stringWithHtml); if (gelezens[position].toString().equals("1")) { chat_right_new1.setImageResource(R.drawable.friend_added_gray); chat_right_new2.setImageResource(R.drawable.friend_added_gray); } else { chat_right_new2.setImageResource(R.drawable.friend_added_gray); chat_right_new1.setImageResource(0); } return convertView; } } private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener { static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (loadedImage != null) { ImageView imageView = (ImageView) view; boolean firstDisplay = !displayedImages.contains(imageUri); if (firstDisplay) { FadeInBitmapDisplayer.animate(imageView, 500); displayedImages.add(imageUri); } } } } @Override public void onResume(Bundle savedInstanceState) { new UpdateList().execute(); super.onNewIntent(savedInstanceState); } @Override public void onNewIntent(Bundle savedInstanceState) { new UpdateList().execute(); super.onNewIntent(savedInstanceState); } private class DeleteConversation extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { UserFunctions userFunctions = new UserFunctions(); JSONObject del_con22 = userFunctions.delConversation(username, password, naam_to_delete); try { if (del_con22.getString("result") != null) { del_con_result = del_con22.getString("result"); } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPreExecute() { progressBar_hole.setVisibility(View.VISIBLE); Toast.makeText(getActivity().getApplicationContext(), "Deleting conversations of " + naam_to_delete, Toast.LENGTH_SHORT).show(); } @Override protected void onPostExecute(Void result) { new UpdateList().execute(); progressBar_hole.setVisibility(View.INVISIBLE); } } private class UpdateList extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { SharedPreferences sp = getActivity().getApplicationContext().getSharedPreferences("loginSaved", Context.MODE_PRIVATE); String naam = sp.getString("naam", null); String username = sp.getString("username", null); String password = sp.getString("password", null); String foto = sp.getString("foto", null); String foto_num = sp.getString("foto_num", null); String friend_list_json = sp.getString("friend_list_json", null); JSONArray json31; UserFunctions userFunctions = new UserFunctions(); json31 = userFunctions.getFriends(username, password, username); Log.d("json31" + json31, "json31" + json31); if (json31.toString().equals("null")) { Log.d("json31" + json31, "json31" + json31); stock_list0.clear(); stock_list.clear(); stock_list2.clear(); stock_list3.clear(); stock_list4.clear(); stock_list5.clear(); stock_list6.clear(); stock_list7.clear(); stock_list8.clear(); stock_list9.clear(); //stock_list9.clear(); stockArr0 = new String[stock_list0.size()]; stockArr = new String[stock_list.size()]; stockArr2 = new String[stock_list2.size()]; stockArr3 = new String[stock_list3.size()]; stockArr4 = new String[stock_list4.size()]; stockArr5 = new String[stock_list5.size()]; stockArr6 = new String[stock_list6.size()]; stockArr7 = new String[stock_list7.size()]; stockArr8 = new String[stock_list8.size()]; stockArr9 = new String[stock_list9.size()]; pids = stock_list9.toArray(stockArr0); gelezens = stock_list9.toArray(stockArr9); is_onlines = stock_list8.toArray(stockArr8); laatste_tijd = stock_list7.toArray(stockArr7); aantal_new_ber = stock_list6.toArray(stockArr6); imageUrls2 = stock_list.toArray(stockArr); names = stock_list2.toArray(stockArr2); laatste_bericht = stock_list3.toArray(stockArr3); fotos = stock_list4.toArray(stockArr4); fotos_nums = stock_list5.toArray(stockArr5); } else { stock_list0 = new ArrayList<String>(); stock_list = new ArrayList<String>(); stock_list2 = new ArrayList<String>(); stock_list3 = new ArrayList<String>(); stock_list4 = new ArrayList<String>(); stock_list5 = new ArrayList<String>(); stock_list6 = new ArrayList<String>(); stock_list7 = new ArrayList<String>(); stock_list8 = new ArrayList<String>(); stock_list9 = new ArrayList<String>(); for (int i = 0; i < json31.length(); i++) { // **line 2** JSONObject childJSONObject = null; try { childJSONObject = json31.getJSONObject(i); String pixx = childJSONObject.getString("buddy"); String name = childJSONObject.getString("naam_vriend"); String foto2 = childJSONObject.getString("foto_vriend"); String foto_mum2 = childJSONObject.getString("num_vriend"); String laatste_berichten2 = childJSONObject.getString("laatste_bericht"); String aantal_new = childJSONObject.getString("aantal_new"); String laatste_bericht_tijd = childJSONObject.getString("laatste_bericht_tijd"); String is_online = childJSONObject.getString("laatst_online"); String gelezen = childJSONObject.getString("gelezen"); stock_list0.add(pixx); stock_list9.add(gelezen); stock_list8.add(is_online); stock_list7.add(laatste_bericht_tijd); stock_list6.add(aantal_new); stock_list4.add(foto2); stock_list5.add(foto_mum2); stock_list2.add(name); stock_list3.add(laatste_berichten2); stock_list.add("http://sjizl.com/fotos/" + foto_mum2 + "/thumbs/" + foto2); //imageUrls[] =url; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } stockArr0 = new String[stock_list0.size()]; stockArr = new String[stock_list.size()]; stockArr2 = new String[stock_list2.size()]; stockArr3 = new String[stock_list3.size()]; stockArr4 = new String[stock_list4.size()]; stockArr5 = new String[stock_list5.size()]; stockArr6 = new String[stock_list6.size()]; stockArr7 = new String[stock_list7.size()]; stockArr8 = new String[stock_list8.size()]; stockArr9 = new String[stock_list9.size()]; pids = stock_list0.toArray(stockArr0); gelezens = stock_list9.toArray(stockArr9); is_onlines = stock_list8.toArray(stockArr8); laatste_tijd = stock_list7.toArray(stockArr7); aantal_new_ber = stock_list6.toArray(stockArr6); imageUrls2 = stock_list.toArray(stockArr); names = stock_list2.toArray(stockArr2); laatste_bericht = stock_list3.toArray(stockArr3); fotos = stock_list4.toArray(stockArr4); fotos_nums = stock_list5.toArray(stockArr5); } return null; } @Override protected void onPreExecute() { progressBar_hole.setVisibility(View.VISIBLE); } @Override protected void onPostExecute(Void result) { adapter.notifyDataSetChanged(); ((ListView) listView).setAdapter(adapter); progressBar_hole.setVisibility(View.INVISIBLE); ((ListView) listView).setChoiceMode(ListView.CHOICE_MODE_SINGLE); adapter.notifyDataSetChanged(); } } private final BroadcastReceiver mHandleMessageReceiver2 = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent i) { if (i != null) { String msg_receive = i.getStringExtra("type"); Bundle bundle = i.getExtras(); String type3 = bundle.getString("type"); String type = "niks"; type = i.getExtras().getString("type"); if (i.getExtras() != null) { if (i.getExtras().containsKey("type")) { type = i.getExtras().getString("type"); if (type.toString().equals("bericht")) { new UpdateList().execute(); } } } } else { } } }; @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int index = info.position; String textTocopy = stock_list3.get(index); switch (item.getItemId()) { case R.id.delete: // quoteResult.remove(info.position); // ((StockQuoteAdapter)getListAdapter()).notifyDataSetChanged(); naam_to_delete = stock_list2.get(info.position); new DeleteConversation().execute(); return false; case R.id.copy: // quoteResult.remove(info.position); // ((StockQuoteAdapter)getListAdapter()).notifyDataSetChanged(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity() .getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("simple text", textTocopy); clipboard.setPrimaryClip(clip); } else { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getActivity() .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(textTocopy); } //place your TextView's text in clipboard Toast.makeText(getActivity().getApplicationContext(), "Item copied", Toast.LENGTH_SHORT).show(); return true; } return false; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.chat_menu_list, menu); } }