Java tutorial
/* * Copyright 2014 Luke Klinker * * 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 com.klinker.android.twitter.ui.search; import android.app.Activity; import android.app.Fragment; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.LinearLayout; import android.widget.ListView; import com.klinker.android.twitter.R; import com.klinker.android.twitter.adapters.ArrayListLoader; import com.klinker.android.twitter.adapters.PeopleArrayAdapter; import com.klinker.android.twitter.data.App; import com.klinker.android.twitter.manipulations.widgets.swipe_refresh_layout.FullScreenSwipeRefreshLayout; import com.klinker.android.twitter.manipulations.widgets.swipe_refresh_layout.SwipeProgressBar; import com.klinker.android.twitter.settings.AppSettings; import com.klinker.android.twitter.utils.Utils; import org.lucasr.smoothie.AsyncListView; import org.lucasr.smoothie.ItemManager; import java.util.ArrayList; import twitter4j.ResponseList; import twitter4j.Twitter; import twitter4j.User; import uk.co.senab.bitmapcache.BitmapLruCache; public class UserSearchFragment extends Fragment { private AsyncListView listView; private LinearLayout spinner; private Context context; private AppSettings settings; private boolean translucent; public String searchQuery; public boolean onlyProfile; private FullScreenSwipeRefreshLayout mPullToRefreshLayout; public UserSearchFragment() { this.translucent = false; this.searchQuery = ""; this.onlyProfile = false; } private BroadcastReceiver newSearch = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { searchQuery = intent.getStringExtra("query"); doUserSearch(searchQuery); } }; @Override public void onResume() { super.onResume(); IntentFilter filter = new IntentFilter(); filter.addAction("com.klinker.android.twitter.NEW_SEARCH"); context.registerReceiver(newSearch, filter); } @Override public void onPause() { context.unregisterReceiver(newSearch); super.onPause(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); context = activity; } public View layout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); this.translucent = getArguments().getBoolean("translucent", false); this.searchQuery = getArguments().getString("search").replaceAll("@", ""); searchQuery = searchQuery.replace(" TOP", ""); this.onlyProfile = getArguments().getBoolean("only_profile", false); settings = AppSettings.getInstance(context); inflater = LayoutInflater.from(context); layout = inflater.inflate(R.layout.ptr_list_layout, null); mPullToRefreshLayout = (FullScreenSwipeRefreshLayout) layout.findViewById(R.id.swipe_refresh_layout); mPullToRefreshLayout.setFullScreen(false); mPullToRefreshLayout.setOnRefreshListener(new FullScreenSwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { onRefreshStarted(); } }); if (settings.addonTheme) { mPullToRefreshLayout.setColorScheme(settings.accentInt, SwipeProgressBar.COLOR2, settings.accentInt, SwipeProgressBar.COLOR3); } else { if (settings.theme != AppSettings.THEME_LIGHT) { mPullToRefreshLayout.setColorScheme(context.getResources().getColor(R.color.app_color), SwipeProgressBar.COLOR2, context.getResources().getColor(R.color.app_color), SwipeProgressBar.COLOR3); } else { mPullToRefreshLayout.setColorScheme(context.getResources().getColor(R.color.app_color), getResources().getColor(R.color.light_ptr_1), context.getResources().getColor(R.color.app_color), getResources().getColor(R.color.light_ptr_2)); } } listView = (AsyncListView) layout.findViewById(R.id.listView); listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { final int lastItem = firstVisibleItem + visibleItemCount; if (lastItem == totalItemCount && canRefresh) { getMoreUsers(searchQuery.replace("@", "")); } } }); if (translucent) { if (Utils.hasNavBar(context)) { View footer = new View(context); footer.setOnClickListener(null); footer.setOnLongClickListener(null); ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, Utils.getNavBarHeight(context)); footer.setLayoutParams(params); listView.addFooterView(footer); listView.setFooterDividersEnabled(false); } } spinner = (LinearLayout) layout.findViewById(R.id.list_progress); spinner.setVisibility(View.GONE); if (searchQuery != null && !searchQuery.equals("") && !searchQuery.contains("@")) { BitmapLruCache cache = App.getInstance(context).getBitmapCache(); ArrayListLoader loader = new ArrayListLoader(cache, context); ItemManager.Builder builder = new ItemManager.Builder(loader); builder.setPreloadItemsEnabled(true).setPreloadItemsCount(10); builder.setThreadPoolSize(2); listView.setItemManager(builder.build()); } doUserSearch(searchQuery); return layout; } public void onRefreshStarted() { mPullToRefreshLayout.setRefreshing(false); } public ArrayList<User> users; public int userPage = 1; public PeopleArrayAdapter peopleAdapter; public void doUserSearch(final String mQuery) { listView.setVisibility(View.GONE); spinner.setVisibility(View.VISIBLE); hasMore = true; canRefresh = false; new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); ResponseList<User> result = twitter.searchUsers(mQuery, userPage); userPage++; if (result.size() < 18) { hasMore = false; canRefresh = false; } users = new ArrayList<User>(); for (User u : result) { users.add(u); } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { peopleAdapter = new PeopleArrayAdapter(context, users, onlyProfile); listView.setAdapter(peopleAdapter); listView.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); canRefresh = true; } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); } }); hasMore = false; canRefresh = true; } } }).start(); } public int toDP(int px) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, getResources().getDisplayMetrics()); } public boolean canRefresh = true; public boolean hasMore; public void getMoreUsers(final String mQuery) { if (hasMore) { canRefresh = false; mPullToRefreshLayout.setRefreshing(true); new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); ResponseList<User> result = twitter.searchUsers(mQuery, userPage); userPage++; for (User u : result) { users.add(u); } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { if (peopleAdapter != null) { peopleAdapter.notifyDataSetChanged(); } mPullToRefreshLayout.setRefreshing(false); } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { mPullToRefreshLayout.setRefreshing(false); } }); hasMore = false; } } }).start(); } } }