This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

Commit b127ba57 by Andrew Karpow

migrated imageviews to volley

parent d78d2322
package de.k4ever.k4android.fragments;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.util.Map;
import android.graphics.Bitmap;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import de.k4ever.k4android.utils.KassenHttpUtils;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -36,9 +45,6 @@ import de.k4ever.k4android.database.KassenContentProvider;
import de.k4ever.k4android.database.KassenSQLiteHelper;
import de.k4ever.k4android.database.KassenSQLiteHelper.Products;
import de.k4ever.k4android.utils.KassenConfig;
import de.k4ever.k4android.utils.KassenHttpImageCache;
import de.k4ever.k4android.utils.KassenHttpUtils.HttpRequestTask;
import de.k4ever.k4android.utils.KassenHttpUtils.HttpTaskResultReceiver;
public class BuyScreenFragment extends Fragment
implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener {
......@@ -46,6 +52,7 @@ implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener {
private TextView mPrice, mDeposit, mNumItems, mDescText, mName;
private double mPriceVal, mDepositVal;
private ImageView mProductImage;
private ImageLoader mImageLoader;
private Button mBuy;
private SeekBar mSeekBar;
private ProgressBar mProgressBar;
......@@ -60,6 +67,8 @@ implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener {
View view = inflater.inflate(R.layout.layout_buyscreen, container, false);
mContainer = view.findViewById(R.id.buyscreenLayout);
mProductImage = (ImageView) view.findViewById(R.id.productImage);
mImageLoader = KassenHttpUtils.getInstance(getActivity()).getImageLoader();
mSeekBar = (SeekBar) view.findViewById(R.id.amount);
mSeekBar.setOnSeekBarChangeListener(this);
mDeposit = (TextView)view.findViewById(R.id.deposit);
......@@ -119,40 +128,54 @@ implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener {
private void buy() {
ArrayList<NameValuePair> request = new ArrayList<NameValuePair>(2);
request.add(new BasicNameValuePair("amount", String.valueOf(mSeekBar.getProgress()+1)));
request.add(new BasicNameValuePair("deposit", ""+mDepositChooser.getSelectedItemPosition()));
new HttpRequestTask(new HttpTaskResultReceiver() {
ProgressDialog dialog;
final ProgressDialog dialog = ProgressDialog.show(getActivity(), "",
getString(R.string.please_wait), true);
String url = KassenHttpUtils.getUrl(KassenConfig.KASSEN_SUBDIR_BUYABLE + mItemID, getActivity());
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject kassenObj) {
dialog.dismiss();
try {
if (kassenObj != null && kassenObj.getBoolean("success")) {
Toast.makeText(getActivity(), getString(R.string.buy_sucess), Toast.LENGTH_LONG).show();
}
else {
// JSON in error case not implemented in api
Toast.makeText(getActivity(), getString(R.string.buy_fail), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// no JSON result: error
Toast.makeText(getActivity(), getString(R.string.buy_fail), Toast.LENGTH_LONG).show();
}
getActivity().finish();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
dialog.dismiss();
Log.e("Volley", "Error: " + volleyError.toString());
Toast.makeText(getActivity(), getString(R.string.buy_fail), Toast.LENGTH_LONG).show();
}
}
) {
@Override
public void onPreExecute() {
dialog = ProgressDialog.show(getActivity(), "",
getString(R.string.please_wait), true);
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> map = KassenHttpUtils
.getAuthHeader(getActivity());
map.put("amount", String.valueOf(mSeekBar.getProgress()+1));
map.put("deposit", ""+mDepositChooser.getSelectedItemPosition());
return map;
}
};
@Override
public void onPostExecute(String result) {
dialog.dismiss();
if(result != "") {
JSONObject kassenObj;
try {
kassenObj = new JSONObject(result);
if (kassenObj != null && kassenObj.getBoolean("success")) {
Toast.makeText(getActivity(), getString(R.string.buy_sucess), Toast.LENGTH_LONG).show();
}
else {
// JSON in error case not implemented in api
Toast.makeText(getActivity(), getString(R.string.buy_fail), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// no JSON result: error
Toast.makeText(getActivity(), getString(R.string.buy_fail), Toast.LENGTH_LONG).show();
}
getActivity().finish();
}
}
}, getActivity()).makePostRequest(request).execute(KassenConfig.KASSEN_SUBDIR_BUYABLE + mItemID);
KassenHttpUtils.getInstance(getActivity()).addToRequestQueue(request);
}
......@@ -166,17 +189,25 @@ implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener {
final String description = cursor.getString(cursor.getColumnIndex(Products.COLUMN_DESCRIPTION));
final int favorite = cursor.getInt(cursor.getColumnIndex(Products.COLUMN_FAVORITE));
final String imgExtension = img.substring(img.lastIndexOf("."));
KassenHttpImageCache imageCache = new KassenHttpImageCache(getActivity(), new HttpTaskResultReceiver() {
mImageLoader.get(KassenHttpUtils.getMediaURL(getActivity()) + img, new ImageLoader.ImageListener() {
@Override
public void onPreExecute() {
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity(), volleyError.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
@Override
public void onPostExecute(String result) {
mProgressBar.setVisibility(View.GONE);
}});
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
if (response != null) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
mProgressBar.setVisibility(View.GONE);
mProductImage.setImageBitmap(bitmap);
}
}
}
});
if(favorite > 0) {
mFavorite = true;
......@@ -194,11 +225,6 @@ implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener {
else
mName.setText(name);
mBuy.setEnabled(true);
imageCache.download(img +
".220x220_q85" +
( imgExtension.equals(".jpeg") ? ".jpg" : imgExtension ),
mProductImage,
String.valueOf(mItemID), "220x220");
}
@Override
......
......@@ -5,7 +5,16 @@ import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import de.k4ever.k4android.utils.KassenConfig;
import de.k4ever.k4android.utils.KassenHttpUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
......@@ -27,10 +36,6 @@ import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import de.k4ever.k4android.R;
import de.k4ever.k4android.utils.KassenConfig;
import de.k4ever.k4android.utils.KassenHttpUtils;
import de.k4ever.k4android.utils.KassenHttpUtils.HttpRequestTask;
import de.k4ever.k4android.utils.KassenHttpUtils.HttpTaskResultReceiver;
public class ManageBalanceFragment extends Fragment {
Menu balanceMenu;
......@@ -101,29 +106,52 @@ public class ManageBalanceFragment extends Fragment {
Toast.makeText(getActivity(), R.string.invalid_sum, Toast.LENGTH_SHORT).show();
}
else {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// amount * -1 to withdraw
if (balanceSpinner.getSelectedItemId() == 1)
amount *= -1;
nameValuePairs.add(new BasicNameValuePair("amount", Float.toString(amount)));
// TODO make 1 charging to constant
nameValuePairs.add(new BasicNameValuePair("type", "1"));
new HttpRequestTask(new HttpTaskResultReceiver() {
ProgressDialog dialog;
final ProgressDialog dialog = ProgressDialog.show(getActivity(), "",
getString(R.string.please_wait), true);
String url = KassenHttpUtils.getUrl(KassenConfig.KASSEN_SUBDIR_TRANSACTIONS, getActivity());
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject kassenObj) {
dialog.dismiss();
updateBalance();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
dialog.dismiss();
Log.e("Volley", "Error: " + volleyError.toString());
Toast.makeText(getActivity(), volleyError.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
) {
@Override
public void onPreExecute() {
dialog = ProgressDialog.show(getActivity(), "", getText(R.string.please_wait), true);
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> map = KassenHttpUtils
.getAuthHeader(getActivity());
NumberFormat numFormat = NumberFormat.getInstance();
float amount = 0;
try {
amount = numFormat.parse(mSum.getText().toString()).floatValue();
} catch (ParseException e) {
e.printStackTrace();
}
map.put("amount", Float.toString(amount));
map.put("type", "1");
return map;
}
};
@Override
public void onPostExecute(String result) {
dialog.dismiss();
updateBalance();
}
}, getActivity()).makePostRequest(nameValuePairs).execute(KassenConfig.KASSEN_SUBDIR_TRANSACTIONS);
KassenHttpUtils.getInstance(getActivity()).addToRequestQueue(request);
}
}
......@@ -134,28 +162,43 @@ public class ManageBalanceFragment extends Fragment {
}
private void updateBalance() {
class BalanceUpdater extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void ... arg0) {
try {
JSONObject res = new JSONObject(
KassenHttpUtils.getPage(getActivity(), KassenConfig.KASSEN_SUBDIR_BALANCE));
return getString(R.string.currency_format, res.getDouble("balance"));
String url = KassenHttpUtils.getUrl(KassenConfig.KASSEN_SUBDIR_BALANCE, getActivity());
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject result) {
try {
if (balanceMenu != null && balanceMenu.findItem(R.id.menu_balance) != null)
balanceMenu
.findItem(R.id.menu_balance)
.setTitle(getString(
R.string.balance_value,
getString(R.string.currency_format, result.getDouble("balance"))
));
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Volley", "Error: " + volleyError.toString());
Toast.makeText(getActivity(), volleyError.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
catch (ClientProtocolException e) { /* */ }
catch (IOException e) { /* */ }
catch (JSONException e) { /* */ }
return "";
}
) {
@Override
protected void onPostExecute(String result) {
if (balanceMenu != null && balanceMenu.findItem(R.id.menu_balance) != null)
balanceMenu.findItem(R.id.menu_balance).setTitle(getString(R.string.balance_value, result));
public Map<String, String> getHeaders() throws AuthFailureError {
return KassenHttpUtils
.getAuthHeader(getActivity());
}
}
BalanceUpdater balanceUpdater = new BalanceUpdater();
balanceUpdater.execute();
};
KassenHttpUtils.getInstance(getActivity()).addToRequestQueue(request);
}
}
......@@ -2,11 +2,15 @@ package de.k4ever.k4android.fragments;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import de.k4ever.k4android.utils.KassenHttpUtils;
import android.app.ProgressDialog;
import android.os.Bundle;
......@@ -26,8 +30,7 @@ import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import de.k4ever.k4android.R;
import de.k4ever.k4android.utils.KassenConfig;
import de.k4ever.k4android.utils.KassenHttpUtils.HttpRequestTask;
import de.k4ever.k4android.utils.KassenHttpUtils.HttpTaskResultReceiver;
import org.json.JSONObject;
public class VirtualsFragment extends Fragment {
EditText mSum, mRecipient, mComment;
......@@ -121,25 +124,50 @@ public class VirtualsFragment extends Fragment {
Toast.makeText(getActivity(), R.string.invalid_recipient, Toast.LENGTH_SHORT).show();
}
else {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("amount", Float.toString(amount)));
nameValuePairs.add(new BasicNameValuePair("recipient", mRecipient.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("comment", mComment.getText().toString()));
new HttpRequestTask(new HttpTaskResultReceiver() {
ProgressDialog dialog;
final ProgressDialog dialog = ProgressDialog.show(getActivity(), "",
getString(R.string.please_wait), true);
String url = KassenHttpUtils.getUrl(KassenConfig.KASSEN_SUBDIR_VIRTUAL, getActivity());
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject result) {
dialog.dismiss();
Toast.makeText(getActivity(), result.toString(), Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
dialog.dismiss();
Log.e("Volley", "Error: " + volleyError.toString());
Toast.makeText(getActivity(), volleyError.toString(), Toast.LENGTH_LONG).show();
}
}
) {
@Override
public void onPreExecute() {
dialog = ProgressDialog.show(getActivity(), "", getText(R.string.please_wait), true);
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> map = KassenHttpUtils.getAuthHeader(getActivity());
NumberFormat numFormat = NumberFormat.getInstance();
float amount = 0;
try {
amount = numFormat.parse(mSum.getText().toString()).floatValue();
} catch (ParseException e) {
e.printStackTrace();
}
map.put("amount", Float.toString(amount));
map.put("recipient", mRecipient.getText().toString());
map.put("comment", mComment.getText().toString());
return map;
}
};
@Override
public void onPostExecute(String result) {
dialog.dismiss();
if(result != "")
Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
}
}, getActivity()).makePostRequest(nameValuePairs).execute(KassenConfig.KASSEN_SUBDIR_VIRTUAL);
KassenHttpUtils.getInstance(getActivity()).addToRequestQueue(request);
}
}
}
......@@ -49,13 +49,17 @@ public class KassenHttpUtils {
return getInstance(context).mAddress + url;
}
public static String getMediaURL(Context context) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString("pref_url", "") + KassenConfig.KASSEN_SUBDIR_MEDIA;
}
private KassenHttpUtils(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
setAPIUrl(prefs.getString("pref_apiurl", ""));
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
......
......@@ -19,28 +19,32 @@ package de.k4ever.k4android.utils;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import de.k4ever.k4android.R;
import de.k4ever.k4android.database.KassenSQLiteHelper.Products;
public class KassenListeAdapter extends CursorAdapter {
private LayoutInflater mInflater;
ImageLoader mImageLoader;
public KassenListeAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = LayoutInflater.from(context);
//mImageCache = new KassenHttpImageCache(context);
mImageLoader = KassenHttpUtils.getInstance(context).getImageLoader();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
NetworkImageView networkImageView = (NetworkImageView) view.findViewById(R.id.networkImageView);
TextView produtTitleView = (TextView) view.findViewById(R.id.producttitle);
TextView priceView = (TextView) view.findViewById(R.id.price);
//TextView depositView = (TextView) view.findViewById(R.id.deposit);
......@@ -50,15 +54,11 @@ public class KassenListeAdapter extends CursorAdapter {
final String name = cursor.getString(cursor.getColumnIndex(Products.COLUMN_NAME));
final Double price = cursor.getDouble(cursor.getColumnIndex(Products.COLUMN_PRICE));
final int favorite = cursor.getInt(cursor.getColumnIndex(Products.COLUMN_FAVORITE));
final String extension = img.substring(img.lastIndexOf("."));
if(imageView != null) {
/*mImageCache.download(img +
KassenConfig.KASSEN_THUMBNAIL_IMAGE_SUFFIX +
( extension.equals(".jpeg") ? ".jpg" : extension ),
imageView,
Integer.toString(id),
"64x64");*/
if(networkImageView != null) {
networkImageView.setImageUrl(
KassenHttpUtils.getMediaURL(context) + img,
mImageLoader);
produtTitleView.setText(name);
if(favorite > 0)
produtTitleView.setTextColor(Color.rgb(0, 100, 0));
......
......@@ -32,7 +32,7 @@
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true" >
<ImageView
<ImageView2
android:id="@+id/productImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
......@@ -20,19 +20,21 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator" >
<ImageView
android:id="@+id/thumbnail"
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/content_image"
android:scaleType="centerInside" />
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/content_image"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:id="@+id/producttitle"
style="@android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/thumbnail"
android:layout_toRightOf="@id/networkImageView"
android:drawableEnd="#00ff00"
android:drawableRight="#00ff00"
android:ellipsize="end"
android:gravity="left"
android:singleLine="true" />
......@@ -42,7 +44,7 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/producttitle"
android:layout_toRightOf="@id/thumbnail"
android:layout_toRightOf="@id/networkImageView"
android:gravity="bottom"
android:singleLine="true" />
</RelativeLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment