Java tutorial
/* * Copyright 2012 The Android Open Source Project * * 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.school.mailclient.app.fragment; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.util.SparseBooleanArray; import android.view.*; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.school.mailclient.app.R; import com.school.mailclient.app.controller.MailManager; import com.school.mailclient.app.model.Coordinate; import com.school.mailclient.app.model.CoordinateInformationObject; import com.school.mailclient.app.model.Mail; import com.school.mailclient.app.util.MailAdapter; import java.text.ParseException; import java.util.ArrayList; import java.util.Vector; public class SentFragment extends Fragment implements AdapterView.OnItemLongClickListener, AbsListView.MultiChoiceModeListener, View.OnClickListener, AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { private ListView sentList = null; public static final String ARG_SECTION_NUMBER = "section_number"; private FloatingActionButton btnMaps; private MailAdapter adapter; private SwipeRefreshLayout swipeRefreshLayout; private MailManager manager; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_sent, container, false); sentList = (ListView) rootView.findViewById(R.id.sentList); swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setOnRefreshListener(this); this.btnMaps = (FloatingActionButton) rootView.findViewById(R.id.btnMaps); this.btnMaps.setOnClickListener(this); btnMaps.hide(); sentList.setOnItemClickListener(this); sentList.setLongClickable(true); sentList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); sentList.setOnItemLongClickListener(this); sentList.setMultiChoiceModeListener(this); Bundle bundle = getActivity().getIntent().getExtras(); if (bundle != null) { manager = (MailManager) bundle.getSerializable("manager"); } swipeRefreshLayout.post(new Runnable() { @Override public void run() { swipeRefreshLayout.setRefreshing(true); LoginValidater task = new LoginValidater(); task.execute(new String[] { "" }); } }); return rootView; } @Override public void onRefresh() { LoginValidater task = new LoginValidater(); task.execute(new String[] { "" }); } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { mode.setTitle(sentList.getCheckedItemCount() + " ausgewhlt"); adapter.toggleSelection(position); } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub return false; } @Override public void onDestroyActionMode(ActionMode mode) { // TODO Auto-generated method stub btnMaps.hide(); } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { //MenuInflater inflater = mode.getMenuInflater(); //inflater.inflate(R.menu.context, menu); btnMaps.show(); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // TODO Auto-generated method stub return false; } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { return true; } private class LoginValidater extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { swipeRefreshLayout.setRefreshing(true); } @Override protected String doInBackground(String... params) { return manager.getOutboxFromServer(); } @Override protected void onPostExecute(String result) { swipeRefreshLayout.setRefreshing(false); if (result != null) { Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show(); } else { updateList(manager.getOutbox()); } } } public void updateList(final ArrayList<Mail> list) { Handler refresh = new Handler(Looper.getMainLooper()); refresh.post(new Runnable() { public void run() { if (list != null) { adapter = new MailAdapter(getActivity(), list); sentList.setAdapter(adapter); } } }); } private ArrayList<Mail> generateEmails() { ArrayList<Mail> mails = new ArrayList<Mail>(); try { Mail m1 = new Mail("1", "1", "Mail 1"); m1.setTitle("Mail 1"); Mail m2 = new Mail("1", "1", "Mail 2"); m2.setTitle("Mail 2"); Mail m3 = new Mail("1", "1", "Mail 3"); m3.setTitle("Mail 3"); Coordinate c1 = new Coordinate(); c1.setLatitude("9.628087"); c1.setLongitude("47.886456"); c1.setIp("192.158.102.1"); Coordinate c2 = new Coordinate(); c2.setLatitude("2.628087"); c2.setLongitude("17.886456"); c2.setIp("192.158.102.1"); Coordinate c3 = new Coordinate(); c3.setLatitude("12.628087"); c3.setLongitude("57.886456"); c3.setIp("192.158.102.1"); Vector<Coordinate> coordinates = new Vector<Coordinate>(); coordinates.add(c1); coordinates.add(c2); coordinates.add(c3); CoordinateInformationObject coordinateObject = new CoordinateInformationObject(); coordinateObject.setCoordinate(coordinates); m1.setCio(coordinateObject); m2.setCio(coordinateObject); m3.setCio(coordinateObject); mails.add(m1); mails.add(m2); mails.add(m3); } catch (ParseException e) { } return mails; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(getContext(), DisplayFragment.class); i.putExtra("mail", adapter.getItemByIndex(position)); startActivity(i); } @Override public void onClick(View v) { ArrayList<Mail> mails = new ArrayList<Mail>(); SparseBooleanArray selected = adapter.getSelectedIds(); short size = (short) selected.size(); for (byte I = 0; I < size; I++) { if (selected.valueAt(I)) { Mail selectedItem = adapter.getItem(selected.keyAt(I)); mails.add(selectedItem); } } Intent i = new Intent(getContext(), MapsActivity.class); i.putExtra("selectedMails", mails); startActivity(i); } }