Advertisement
msveemac

AdvancedActivity

Aug 28th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.51 KB | None | 0 0
  1. package com.vanessaem.FlowIcons;
  2.  
  3. import java.util.List;
  4. import java.util.Locale;
  5.  
  6. import android.animation.Animator;
  7. import android.animation.Animator.AnimatorListener;
  8. import android.animation.AnimatorInflater;
  9. import android.animation.AnimatorSet;
  10. import android.annotation.SuppressLint;
  11. import android.app.Activity;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.content.res.Resources;
  15. import android.os.Build;
  16. import android.os.Bundle;
  17. import android.os.Environment;
  18. import android.os.Handler;
  19. import android.support.v4.app.FragmentActivity;
  20. import android.util.SparseBooleanArray;
  21. import android.util.TypedValue;
  22. import android.view.KeyEvent;
  23. import android.view.LayoutInflater;
  24. import android.view.Menu;
  25. import android.view.MenuItem;
  26. import android.view.View;
  27. import android.view.View.OnClickListener;
  28. import android.view.View.OnLongClickListener;
  29. import android.view.ViewGroup;
  30. import android.view.inputmethod.InputMethodManager;
  31. import android.widget.AdapterView;
  32. import android.widget.AdapterView.OnItemClickListener;
  33. import android.widget.BaseAdapter;
  34. import android.widget.FrameLayout;
  35. import android.widget.GridView;
  36. import android.widget.ImageButton;
  37. import android.widget.ImageView;
  38. import android.widget.LinearLayout;
  39. import android.widget.SearchView;
  40. import android.widget.SearchView.OnQueryTextListener;
  41. import android.widget.TextView;
  42. import android.widget.Toast;
  43.  
  44. import com.pkmmte.requestmanager.AppInfo;
  45. import com.pkmmte.requestmanager.AppLoadListener;
  46. import com.pkmmte.requestmanager.PkRequestManager;
  47. import com.pkmmte.requestmanager.RequestSettings;
  48. import com.pkmmte.requestmanager.SendRequestListener;
  49. import com.vanessaem.FlowIcons.R;
  50. import com.vanessaem.FlowIcons.QuickScroll.Scrollable;
  51.  
  52. public class AdvancedActivity extends Activity implements OnItemClickListener, OnQueryTextListener, AppLoadListener, SendRequestListener
  53. {
  54. // Request Manager
  55. private PkRequestManager mRequestManager;
  56.  
  57. // AppInfo List & Adapter
  58. private List<AppInfo> mApps;
  59. private RequestAdapter mAdapter;
  60.  
  61. // UI Handler
  62. private Handler mHandler;
  63.  
  64. // Views
  65. private SearchView searchView;
  66. private GridView mGrid;
  67. private QuickScroll mScroll;
  68. private LinearLayout containerLoading;
  69. private TextView txtLoading;
  70. private TextView txtSelected;
  71. private ImageButton btnSelectAll;
  72. private ImageButton btnDeselectAll;
  73.  
  74. @Override
  75. protected void onCreate(Bundle savedInstanceState)
  76. {
  77. super.onCreate(savedInstanceState);
  78. setContentView(R.layout.activity_advanced);
  79. getActionBar().setDisplayHomeAsUpEnabled(true);
  80.  
  81. // Initialize your layout views
  82. initViews();
  83.  
  84. // Initialize your PkRequestManager
  85. initRequestManager();
  86.  
  87. // Initialize your UI Handler. This is for modifying your UI from a background thread
  88. mHandler = new Handler();
  89. }
  90.  
  91. @Override
  92. public void onStart()
  93. {
  94. super.onStart();
  95.  
  96. // Event listeners to notify us when a task has finished executing
  97. setListeners();
  98.  
  99. // Asynchronously load apps if they're not already loaded.
  100. mRequestManager.loadAppsIfEmptyAsync();
  101.  
  102. // Get a list of apps and try populating, even if they're not loaded
  103. mApps = mRequestManager.getApps();
  104. populateGrid();
  105. }
  106.  
  107. @Override
  108. public void onStop()
  109. {
  110. super.onStop();
  111.  
  112. // Remove listeners when the app isn't visible to prevent any issues
  113. removeListeners();
  114. }
  115.  
  116. @Override
  117. public boolean onCreateOptionsMenu(Menu menu)
  118. {
  119. getMenuInflater().inflate(R.menu.request_advanced, menu);
  120.  
  121. return true;
  122. }
  123.  
  124. @Override
  125. public boolean onOptionsItemSelected(MenuItem item)
  126. {
  127. switch (item.getItemId())
  128. {
  129. case android.R.id.home:
  130. finish();
  131. return true;
  132. case R.id.send:
  133. // Small workaround
  134. mRequestManager.setActivity(this);
  135.  
  136. if(mRequestManager.getNumSelected() > 0) {
  137. // Build and send the request in the background if apps are selected.
  138. mRequestManager.sendRequestAsync();
  139. Toast.makeText(this, getString(R.string.building_request), Toast.LENGTH_LONG).show();
  140. }
  141. else {
  142. // Show error message if no apps are selected
  143. Toast.makeText(this, getString(R.string.none_selected), Toast.LENGTH_LONG).show();
  144. }
  145.  
  146. return true;
  147. default:
  148. return super.onOptionsItemSelected(item);
  149. }
  150. }
  151.  
  152.  
  153. private void initViews()
  154. {
  155. mGrid = (GridView) findViewById(R.id.appGrid);
  156. mScroll = (QuickScroll) findViewById(R.id.quickScroll);
  157. containerLoading = (LinearLayout) findViewById(R.id.Loading);
  158. txtLoading = (TextView) findViewById(R.id.txtLoading);
  159. txtSelected = (TextView) findViewById(R.id.txtNumSelected);
  160. btnSelectAll = (ImageButton) findViewById(R.id.btnSelectAll);
  161. btnDeselectAll = (ImageButton) findViewById(R.id.btnDeselectAll);
  162. }
  163.  
  164. private void initRequestManager()
  165. {
  166. // Grab a reference to the manager and store it in a variable. This helps make code shorter.
  167. mRequestManager = PkRequestManager.getInstance(this);
  168.  
  169. // Enable debugging. Disable this during production!
  170. mRequestManager.setDebugging(true);
  171.  
  172. // Set your custom settings.
  173. mRequestManager.setSettings(new RequestSettings.Builder()
  174. .addEmailAddress(getString(R.string.developer_email)) // Email where the request will be sent to
  175. .emailSubject(getString(R.string.request_subject)) // Email Subject
  176. .emailPrecontent("These apps are missing on my phone:\n\n") // Text before the main app information
  177. .saveLocation(Environment.getExternalStorageDirectory().getAbsolutePath() + "/mytheme/.icon_request") // Location to where the .zips and temporary files will be saved
  178. .appfilterName("appfilter.xml") // Specify your appfilter.xml name if it's different from the standard. This will be used to filter out apps from the list.
  179. .compressFormat(PkRequestManager.PNG) // Compression format for the attached app icons
  180. .appendInformation(true) // Choose whether or not you'd like to receive information about the user's device such as OS version, manufacturer, model number, build, etc.
  181. .createAppfilter(true) // True if you'd like to automatically generate an appfilter.xml for the requested apps
  182. .createZip(true) // True if you want to receive app icons with the email
  183. .filterAutomatic(true) // True if you want apps you support in your appfilter.xml to be filtered out from automatic requests
  184. .filterDefined(true) // True if you don't want apps you already defined in your appfilter.xml to show up in the app list
  185. .byteBuffer(2048) // Buffer size in bytes for writing to memory.
  186. .compressQuality(100) // Compression quality for attached app icons
  187. .build());
  188. }
  189.  
  190. private void setListeners()
  191. {
  192. // Load & Request Listeners
  193. mRequestManager.addAppLoadListener(this);
  194. mRequestManager.addSendRequestListener(this);
  195.  
  196. // Select/Deselect All Button Listeners
  197. btnSelectAll.setOnClickListener(new OnClickListener() {
  198. @Override
  199. public void onClick(View v) {
  200. // Return if not yet ready
  201. if(mApps == null || mAdapter == null)
  202. return;
  203.  
  204. // Keep track of previously selected apps for animation
  205. SparseBooleanArray selectedApps = new SparseBooleanArray();
  206. int size1 = mApps.size();
  207. for(int i = 0; i < size1; i++) {
  208. selectedApps.put(i, mApps.get(i).isSelected());
  209. }
  210.  
  211. // Select all
  212. mRequestManager.selectAll();
  213.  
  214. // Animate
  215. int size2 = mApps.size();
  216. for(int i = 0; i < size2; i++) {
  217. if(!selectedApps.get(i))
  218. mAdapter.animateView(i, mGrid);
  219. }
  220.  
  221. // Update Number of apps selected
  222. int numSelected = mRequestManager.getNumSelected();
  223. txtSelected.setText(getResources().getQuantityString(R.plurals.num_apps_selected, numSelected, numSelected));
  224. }
  225. });
  226. btnDeselectAll.setOnClickListener(new OnClickListener() {
  227. @Override
  228. public void onClick(View v) {
  229. // Return if not yet ready
  230. if(mApps == null || mAdapter == null)
  231. return;
  232.  
  233. // Keep track of previously selected apps for animation
  234. SparseBooleanArray selectedApps = new SparseBooleanArray();
  235. int size1 = mApps.size();
  236. for(int i = 0; i < size1; i++) {
  237. selectedApps.put(i, mApps.get(i).isSelected());
  238. }
  239.  
  240. // Deselect all
  241. mRequestManager.deselectAll();
  242.  
  243. // Animate
  244. int size = mApps.size();
  245. for(int i = 0; i < size; i++) {
  246. if(selectedApps.get(i))
  247. mAdapter.animateView(i, mGrid);
  248. }
  249.  
  250. // Update Number of apps selected
  251. int numSelected = mRequestManager.getNumSelected();
  252. txtSelected.setText(getResources().getQuantityString(R.plurals.num_apps_selected, numSelected, numSelected));
  253. }
  254. });
  255.  
  256. // Fancy toast
  257. btnSelectAll.setOnLongClickListener(new OnLongClickListener() {
  258. @Override
  259. public boolean onLongClick(View v) {
  260. Toast.makeText(AdvancedActivity.this, v.getContentDescription(), Toast.LENGTH_SHORT).show();
  261. return true;
  262. }
  263. });
  264. btnDeselectAll.setOnLongClickListener(new OnLongClickListener() {
  265. @Override
  266. public boolean onLongClick(View v) {
  267. Toast.makeText(AdvancedActivity.this, v.getContentDescription(), Toast.LENGTH_SHORT).show();
  268. return true;
  269. }
  270. });
  271. }
  272.  
  273. private void removeListeners()
  274. {
  275. mRequestManager.removeAppLoadListener(this);
  276. mRequestManager.removeSendRequestListener(this);
  277. btnSelectAll.setOnClickListener(null);
  278. btnDeselectAll.setOnClickListener(null);
  279. }
  280.  
  281. private void populateGrid()
  282. {
  283. // Do nothing if app list is null or empty
  284. if(mApps == null || mApps.size() == 0)
  285. return;
  286.  
  287. // Hide loading views
  288. containerLoading.setVisibility(View.GONE);
  289.  
  290. // Populate the GridView
  291. mAdapter = new RequestAdapter(this, mApps);
  292. mGrid.setAdapter(mAdapter);
  293. mGrid.setOnItemClickListener(this);
  294. mGrid.setVisibility(View.VISIBLE);
  295.  
  296. // Initialize & Configure QuickScroll
  297. mScroll.init(QuickScroll.TYPE_INDICATOR_WITH_HANDLE, mGrid, mAdapter, QuickScroll.STYLE_HOLO);
  298. mScroll.setFixedSize(1);
  299. mScroll.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
  300. mScroll.setVisibility(View.VISIBLE);
  301.  
  302. // Update Number of apps selected (if any were already selected)
  303. int numSelected = mRequestManager.getNumSelected();
  304. txtSelected.setText(getResources().getQuantityString(R.plurals.num_apps_selected, numSelected, numSelected));
  305. }
  306.  
  307. @Override
  308. public void onItemClick(AdapterView<?> parent, View view, int position, long id)
  309. {
  310. // Trigger fancy animation
  311. mAdapter.animateView(position, mGrid);
  312.  
  313. // Mark the app as selected
  314. AppInfo mApp = mApps.get(position);
  315. mApp.setSelected(!mApp.isSelected());
  316. mApps.set(position, mApp);
  317.  
  318. // Let the adapter know you selected something
  319. //mAdapter.notifyDataSetChanged();
  320.  
  321. // Update Number of apps selected
  322. int numSelected = mRequestManager.getNumSelected();
  323. txtSelected.setText(getResources().getQuantityString(R.plurals.num_apps_selected, numSelected, numSelected));
  324. }
  325.  
  326. @Override
  327. public boolean onQueryTextSubmit(String query)
  328. {
  329.  
  330.  
  331. // Hide Keyboard
  332. ((InputMethodManager) getSystemService(FragmentActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(searchView.getWindowToken(), 0);
  333.  
  334. return false;
  335. }
  336.  
  337.  
  338. public boolean onQueryTextChange(String newText)
  339. {
  340.  
  341. return false;
  342. }
  343.  
  344.  
  345. public void onAppPreload()
  346. {
  347. mHandler.post(new Runnable(){
  348.  
  349. public void run() {
  350. if(containerLoading.isShown())
  351. txtLoading.setText(getResources().getString(R.string.preparing_to_load));
  352. }
  353. });
  354. }
  355.  
  356. @Override
  357. public void onAppLoading(final int status, final int progress)
  358. {
  359. mHandler.post(new Runnable(){
  360. @Override
  361. public void run() {
  362. if(containerLoading.isShown())
  363. txtLoading.setText("Loading... " + progress + "%");
  364. }
  365. });
  366. }
  367.  
  368. @Override
  369. public void onAppLoaded()
  370. {
  371. mHandler.post(new Runnable(){
  372. @Override
  373. public void run() {
  374. mApps = mRequestManager.getApps();
  375. populateGrid();
  376. }
  377. });
  378. }
  379.  
  380. @Override
  381. public void onRequestStart(boolean automatic)
  382. {
  383.  
  384. }
  385.  
  386. @Override
  387. public void onRequestBuild(boolean automatic, int progress)
  388. {
  389.  
  390. }
  391.  
  392. @Override
  393. public void onRequestFinished(boolean automatic, boolean intentSuccessful, Intent intent)
  394. {
  395.  
  396. }
  397.  
  398. // You should probably put this in a separate .java file
  399. private class RequestAdapter extends BaseAdapter implements Scrollable
  400. {
  401. private Context mContext;
  402. private List<AppInfo> mApps;
  403. private Resources mRes;
  404.  
  405. public RequestAdapter(Context context, List<AppInfo> apps)
  406. {
  407. this.mContext = context;
  408. this.mApps = apps;
  409. this.mRes = context.getResources();
  410. }
  411.  
  412. @Override
  413. public int getCount()
  414. {
  415. return this.mApps.size();
  416. }
  417.  
  418. @Override
  419. public AppInfo getItem(int position)
  420. {
  421. return this.mApps.get(position);
  422. }
  423.  
  424. @Override
  425. public long getItemId(int position)
  426. {
  427. return position;
  428. }
  429.  
  430. @Override
  431. public String getIndicatorForPosition(int childposition, int groupposition)
  432. {
  433. return Character.toString(this.mApps.get(childposition).getName().charAt(0)).toUpperCase(Locale.getDefault());
  434. }
  435.  
  436. @Override
  437. public int getScrollPosition(int childposition, int groupposition)
  438. {
  439. return childposition;
  440. }
  441.  
  442. @Override
  443. public View getView(int position, View convertView, ViewGroup parent)
  444. {
  445. ViewHolder holder;
  446. AppInfo mApp = this.mApps.get(position);
  447.  
  448. if (convertView == null)
  449. {
  450. LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  451. convertView = inflater.inflate(R.layout.activity_advanced_item, null);
  452.  
  453. holder = new ViewHolder();
  454. holder.txtCode = (TextView) convertView.findViewById(R.id.txtCode);
  455. holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
  456. holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
  457.  
  458. holder.Card = (FrameLayout) convertView.findViewById(R.id.Card);
  459. holder.btnContainer = (FrameLayout) convertView.findViewById(R.id.btnIconContainer);
  460. holder.imgSelected = (ImageView) convertView.findViewById(R.id.imgSelected);
  461. holder.bgSelected = convertView.findViewById(R.id.bgSelected);
  462.  
  463. convertView.setTag(holder);
  464. }
  465. else {
  466. holder = (ViewHolder) convertView.getTag();
  467. }
  468.  
  469. holder.txtName.setText(mApp.getName());
  470. holder.txtCode.setText(mApp.getCode());
  471. holder.imgIcon.setImageDrawable(mApp.getImage());
  472.  
  473. if(mApp.isSelected()) {
  474. selectCard(true, holder.Card);
  475. holder.bgSelected.setVisibility(View.VISIBLE);
  476. holder.imgSelected.setVisibility(View.VISIBLE);
  477. }
  478. else {
  479. selectCard(false, holder.Card);
  480. holder.bgSelected.setVisibility(View.GONE);
  481. holder.imgSelected.setVisibility(View.GONE);
  482. }
  483.  
  484. return convertView;
  485. }
  486.  
  487. @SuppressLint("NewApi")
  488. @SuppressWarnings("deprecation")
  489. private void selectCard(boolean Selected, FrameLayout Card)
  490. {
  491. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
  492. Card.setBackgroundDrawable(Selected ? mRes.getDrawable(R.drawable.card_bg_selected) : mRes.getDrawable(R.drawable.card_bg));
  493. else
  494. Card.setBackground(Selected ? mRes.getDrawable(R.drawable.card_bg_selected) : mRes.getDrawable(R.drawable.card_bg));
  495. }
  496.  
  497. public void animateView(int position, GridView grid)
  498. {
  499. try {
  500. View v = grid.getChildAt(position - grid.getFirstVisiblePosition());
  501.  
  502. ViewHolder holder = new ViewHolder();
  503. holder.Card = (FrameLayout) v.findViewById(R.id.Card);
  504. holder.btnContainer = (FrameLayout) v.findViewById(R.id.btnIconContainer);
  505. holder.imgIcon = (ImageView) v.findViewById(R.id.imgIcon);
  506. holder.imgSelected = (ImageView) v.findViewById(R.id.imgSelected);
  507. holder.bgSelected = v.findViewById(R.id.bgSelected);
  508.  
  509. if (this.mApps.get(position).isSelected())
  510. animateAppDeselected(holder);
  511. else
  512. animateAppSelected(holder);
  513. }
  514. catch(Exception e) {
  515. // View not visible
  516. }
  517. }
  518.  
  519. private void animateAppSelected(final ViewHolder holderFinal)
  520. {
  521. // Declare AnimatorSets
  522. final AnimatorSet animOut = (AnimatorSet) AnimatorInflater.loadAnimator(mContext, R.anim.card_flip_right_out);
  523. final AnimatorSet animIn = (AnimatorSet) AnimatorInflater.loadAnimator(mContext, R.anim.card_flip_left_in);
  524. animOut.setTarget(holderFinal.btnContainer);
  525. animIn.setTarget(holderFinal.btnContainer);
  526. animOut.addListener(new AnimatorListener() {
  527. @Override
  528. public void onAnimationCancel(Animator animation) {
  529. // Nothing
  530. }
  531.  
  532. @Override
  533. public void onAnimationEnd(Animator animation) {
  534. selectCard(true, holderFinal.Card);
  535. holderFinal.bgSelected.setVisibility(View.VISIBLE);
  536. holderFinal.imgSelected.setVisibility(View.VISIBLE);
  537. notifyDataSetChanged();
  538. animIn.start();
  539. }
  540.  
  541. @Override
  542. public void onAnimationRepeat(Animator animation) {
  543. // Nothing
  544. }
  545.  
  546. @Override
  547. public void onAnimationStart(Animator animation) {
  548. selectCard(false, holderFinal.Card);
  549. holderFinal.bgSelected.setVisibility(View.GONE);
  550. holderFinal.imgSelected.setVisibility(View.GONE);
  551. }
  552. });
  553. animOut.start();
  554. }
  555.  
  556. private void animateAppDeselected(final ViewHolder holderFinal)
  557. {
  558. // Declare AnimatorSets
  559. final AnimatorSet animOut = (AnimatorSet) AnimatorInflater.loadAnimator(mContext, R.anim.card_flip_left_out);
  560. final AnimatorSet animIn = (AnimatorSet) AnimatorInflater.loadAnimator(mContext, R.anim.card_flip_right_in);
  561. animOut.setTarget(holderFinal.btnContainer);
  562. animIn.setTarget(holderFinal.btnContainer);
  563. animOut.addListener(new AnimatorListener() {
  564. @Override
  565. public void onAnimationCancel(Animator animation) {
  566. // Nothing
  567. }
  568.  
  569. @Override
  570. public void onAnimationEnd(Animator animation) {
  571. selectCard(false, holderFinal.Card);
  572. holderFinal.bgSelected.setVisibility(View.GONE);
  573. holderFinal.imgSelected.setVisibility(View.GONE);
  574. notifyDataSetChanged();
  575. animIn.start();
  576. }
  577.  
  578. @Override
  579. public void onAnimationRepeat(Animator animation) {
  580. // Nothing
  581. }
  582.  
  583. @Override
  584. public void onAnimationStart(Animator animation) {
  585. selectCard(true, holderFinal.Card);
  586. holderFinal.bgSelected.setVisibility(View.VISIBLE);
  587. holderFinal.imgSelected.setVisibility(View.VISIBLE);
  588. }
  589. });
  590. animOut.start();
  591. }
  592.  
  593. private class ViewHolder
  594. {
  595. public TextView txtCode;
  596. public TextView txtName;
  597. public ImageView imgIcon;
  598.  
  599. public FrameLayout Card;
  600. public FrameLayout btnContainer;
  601. public ImageView imgSelected;
  602. public View bgSelected;
  603. }
  604. }
  605. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement