Created
May 19, 2014 21:28
-
-
Save coffeearmy/c87aa7e611f6a5f3fb69 to your computer and use it in GitHub Desktop.
TestProgram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class APIHandler { | |
private static final String API_URL = "http://api2.softonic.com"; | |
private static RestAdapter restAdapter; | |
public APIHandler() {} | |
private static RestAdapter getRestAdapter(){ | |
if(restAdapter==null){ | |
restAdapter = new RestAdapter.Builder() | |
.setEndpoint(API_URL) | |
.build(); | |
} | |
return restAdapter; | |
} | |
public static SoftonicAPI getApiInterface(){ | |
// Create an instance of our API interface. | |
SoftonicAPI sfAPI=null; | |
try { | |
if(restAdapter==null){ | |
restAdapter=getRestAdapter(); | |
} | |
sfAPI = restAdapter.create(SoftonicAPI.class); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return sfAPI; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.rest; | |
import android.content.Context; | |
import com.coffeearmy.softtest.pojo.Platforms; | |
import com.coffeearmy.softtest.pojo.Programs; | |
import com.coffeearmy.softtest.rest.SoftonicAPI.Format; | |
import com.coffeearmy.softtest.rest.SoftonicAPI.Instance; | |
import retrofit.Callback; | |
public class APIResponse { | |
private static final String API_KEY = "5c3e53e96ef2f41e8eec74ca93886af4"; | |
private static APIResponse mAPIResponse; | |
private Instance mInstance; | |
private Format mFormat; | |
public static APIResponse getInstance(Context mContext) { | |
if(mAPIResponse==null){ | |
mAPIResponse= new APIResponse(); | |
} | |
return mAPIResponse; | |
} | |
public APIResponse() { | |
// Default Value | |
mInstance = SoftonicAPI.Instance.en; | |
mFormat = SoftonicAPI.Format.json; | |
} | |
public void getSections(Callback<Platforms> callback) { | |
// Create an instance of our API interface. | |
SoftonicAPI responseAPI = APIHandler.getApiInterface(); | |
// Fetch and print a list of the contributors to this library. | |
responseAPI.getSection(mInstance, mFormat, API_KEY, callback); | |
} | |
public Programs getPrograms(String section){ | |
SoftonicAPI responseAPI = APIHandler.getApiInterface(); | |
return responseAPI.getPrograms(mInstance, section, mFormat, API_KEY); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.pojo; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Platforms { | |
private String status; | |
private Integer total_items; | |
private List<Platform> platforms = new ArrayList<Platform>(); | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public Integer getTotal_items() { | |
return total_items; | |
} | |
public void setTotal_items(Integer total_items) { | |
this.total_items = total_items; | |
} | |
public List<Platform> getPlatforms() { | |
return platforms; | |
} | |
public void setPlatforms(List<Platform> platforms) { | |
this.platforms = platforms; | |
} | |
public class Platform { | |
private PlatformItem platform; | |
public PlatformItem getPlatform() { | |
return platform; | |
} | |
public void setPlatform(PlatformItem platform) { | |
this.platform = platform; | |
} | |
} | |
public class PlatformItem { | |
private Integer id_section; | |
private String name; | |
public Integer getId_section() { | |
return id_section; | |
} | |
public void setId_section(Integer id_section) { | |
this.id_section = id_section; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.fragment; | |
import java.util.ArrayList; | |
import java.util.List; | |
import retrofit.Callback; | |
import retrofit.RetrofitError; | |
import retrofit.client.Response; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.AdapterView.OnItemClickListener; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import com.coffeearmy.softtest.R; | |
import com.coffeearmy.softtest.adapter.PlatfromListAdapter; | |
import com.coffeearmy.softtest.pojo.Platforms; | |
import com.coffeearmy.softtest.pojo.Platforms.Platform; | |
import com.coffeearmy.softtest.rest.APIResponse; | |
public class PlatformsListFragment extends Fragment { | |
public static final String FRAGMENT_TAG = "platform_list_fragment"; | |
private static PlatformsListFragment mPlatformFrag; | |
private PlatfromListAdapter mAdapterList; | |
public static PlatformsListFragment getInstance() { | |
if (mPlatformFrag == null) { | |
mPlatformFrag = new PlatformsListFragment(); | |
} | |
return mPlatformFrag; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
ListView mListView = new ListView(getActivity()); | |
mAdapterList = new PlatfromListAdapter(getActivity(), | |
android.R.layout.simple_list_item_1, | |
new ArrayList<Platforms.Platform>()); | |
mListView.setAdapter(mAdapterList); | |
mListView.setOnItemClickListener(new OnPlatformClick()); | |
populateListWithPlatforms(); | |
return mListView; | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
} | |
private void populateListWithPlatforms() { | |
APIResponse response = new APIResponse(); | |
response.getSections(new Callback<Platforms>() { | |
@Override | |
public void success(Platforms arg0, Response arg1) { | |
Log.i("Result", arg0.getTotal_items() + ""); | |
ChangeDataFromList(arg0.getPlatforms()); | |
} | |
@Override | |
public void failure(RetrofitError arg0) { | |
Toast.makeText(getActivity(), "OHNOES", Toast.LENGTH_LONG) | |
.show(); | |
} | |
}); | |
} | |
private void ChangeDataFromList(List<Platform> platform) { | |
mAdapterList.changeData(platform); | |
} | |
public void goToDetailFragment(View arg1) { | |
ProgramListFragment f = new ProgramListFragment(); | |
PlatfromListAdapter.ViewHolder holder = (PlatfromListAdapter.ViewHolder) arg1 | |
.getTag(); | |
Bundle b = new Bundle(); | |
b.putString(ProgramListFragment.SECTION_ID, holder.getIdPlatfrom() + ""); | |
f.setArguments(b); | |
getActivity() | |
.getSupportFragmentManager() | |
.beginTransaction() | |
.replace(R.id.fragmentContainer, f, | |
ProgramListFragment.FRAGMENT_TAG).addToBackStack(null) | |
.commit(); | |
} | |
/** On click listener for choosing a icon for the operatio */ | |
protected class OnPlatformClick implements OnItemClickListener { | |
@Override | |
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, | |
long arg3) { | |
goToDetailFragment(arg1); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.adapter; | |
import java.util.List; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.coffeearmy.softtest.R; | |
import com.coffeearmy.softtest.pojo.Programs.Program; | |
import com.nostra13.universalimageloader.core.DisplayImageOptions; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
public class ProgramListAdaper extends ArrayAdapter<Program> { | |
private Context mContext; | |
private int mItemLayout; | |
private List<Program> mDataList; | |
private ImageLoader mImageLoader; | |
private DisplayImageOptions options; | |
// ViewHolder pattern | |
static class ViewHolder { | |
TextView namePlatform; | |
String idProgram; | |
String urlProgram; | |
ImageView imgBG; | |
public String getIdPlatfrom() { | |
return idProgram; | |
} | |
} | |
public ProgramListAdaper(Context context, int textViewResourceId, | |
List<Program> objects) { | |
super(context, textViewResourceId, objects); | |
mContext = context; | |
mItemLayout = textViewResourceId; | |
mDataList = objects; | |
mImageLoader=ImageLoader.getInstance(); | |
options = new DisplayImageOptions.Builder() | |
.showStubImage(R.drawable.ic_launcher) | |
.showImageForEmptyUri(R.drawable.ic_launcher) | |
.cacheOnDisc() | |
.cacheInMemory() | |
.build(); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View rowView = convertView; | |
// If the convertview is null needs to be inflated, if not null it can | |
// be reuse it | |
if (rowView == null) { | |
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater(); | |
rowView = inflater.inflate(mItemLayout, null); | |
// configure view holder | |
ViewHolder viewHolder = new ViewHolder(); | |
viewHolder.namePlatform = (TextView) rowView | |
.findViewById(R.id.txtVNameProgram); | |
viewHolder.imgBG = (ImageView) rowView.findViewById(R.id.bgImage); | |
// Store the viewHolder in the tag of the view | |
rowView.setTag(viewHolder); | |
} | |
// fill data | |
ViewHolder holder = (ViewHolder) rowView.getTag(); | |
Program item = mDataList.get(position); | |
holder.idProgram = item.getProgram().getId_program(); | |
holder.namePlatform.setText(item.getProgram().getTitle()); | |
holder.urlProgram = item.getProgram().getUrl(); | |
String imgUrl=item.getProgram().getImg(); | |
// Load image, decode it to Bitmap and display Bitmap in ImageView (or | |
// any other view which implements ImageAware interface) | |
// Using universal-imager-loader lib | |
if (imgUrl != null && !imgUrl.equals("")) { | |
mImageLoader.displayImage(imgUrl, holder.imgBG,options); | |
} | |
return rowView; | |
} | |
public void changeDataSet(List<Program> arg1) { | |
mDataList.clear(); | |
mDataList.addAll(arg1); | |
notifyDataSetChanged(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.pojo; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class Programs { | |
private String status; | |
private String total_items; | |
private List<Program> programs = new ArrayList<Program>(); | |
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public String getTotal_items() { | |
return total_items; | |
} | |
public void setTotal_items(String total_items) { | |
this.total_items = total_items; | |
} | |
public List<Program> getPrograms() { | |
return programs; | |
} | |
public void setPrograms(List<Program> programs) { | |
this.programs = programs; | |
} | |
public Map<String, Object> getAdditionalProperties() { | |
return this.additionalProperties; | |
} | |
public void setAdditionalProperty(String name, Object value) { | |
this.additionalProperties.put(name, value); | |
} | |
public class ProgramItem { | |
private String id_program; | |
private String title; | |
private String description; | |
private String version; | |
private String rating_softonic; | |
private String rating_user; | |
private String date_updated; | |
private String date_added; | |
private String license_type; | |
private String last_update; | |
private String url; | |
private String img; | |
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | |
public String getId_program() { | |
return id_program; | |
} | |
public void setId_program(String id_program) { | |
this.id_program = id_program; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public String getVersion() { | |
return version; | |
} | |
public void setVersion(String version) { | |
this.version = version; | |
} | |
public String getRating_softonic() { | |
return rating_softonic; | |
} | |
public void setRating_softonic(String rating_softonic) { | |
this.rating_softonic = rating_softonic; | |
} | |
public String getRating_user() { | |
return rating_user; | |
} | |
public void setRating_user(String rating_user) { | |
this.rating_user = rating_user; | |
} | |
public String getDate_updated() { | |
return date_updated; | |
} | |
public void setDate_updated(String date_updated) { | |
this.date_updated = date_updated; | |
} | |
public String getDate_added() { | |
return date_added; | |
} | |
public void setDate_added(String date_added) { | |
this.date_added = date_added; | |
} | |
public String getLicense_type() { | |
return license_type; | |
} | |
public void setLicense_type(String license_type) { | |
this.license_type = license_type; | |
} | |
public String getLast_update() { | |
return last_update; | |
} | |
public void setLast_update(String last_update) { | |
this.last_update = last_update; | |
} | |
public String getUrl() { | |
return url; | |
} | |
public void setUrl(String url) { | |
this.url = url; | |
} | |
public String getImg() { | |
return img; | |
} | |
public void setImg(String img) { | |
this.img = img; | |
} | |
public Map<String, Object> getAdditionalProperties() { | |
return this.additionalProperties; | |
} | |
public void setAdditionalProperty(String name, Object value) { | |
this.additionalProperties.put(name, value); | |
} | |
} | |
public class Program { | |
private ProgramItem program; | |
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | |
public ProgramItem getProgram() { | |
return program; | |
} | |
public void setProgram(ProgramItem program) { | |
this.program = program; | |
} | |
public Map<String, Object> getAdditionalProperties() { | |
return this.additionalProperties; | |
} | |
public void setAdditionalProperty(String name, Object value) { | |
this.additionalProperties.put(name, value); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.loader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.coffeearmy.softtest.pojo.Platforms.PlatformItem; | |
import com.coffeearmy.softtest.pojo.Programs; | |
import com.coffeearmy.softtest.pojo.Programs.Program; | |
import com.coffeearmy.softtest.rest.APIResponse; | |
import android.content.Context; | |
import android.support.v4.content.AsyncTaskLoader; | |
public class ProgramsLoader extends AsyncTaskLoader<List<Program>>{ | |
private Context mContext; | |
private String mSection; | |
public ProgramsLoader(Context context,String section) { | |
super(context); | |
mContext=context; | |
mSection=section; | |
} | |
@Override | |
public List<Program> loadInBackground() { | |
List<Program> result; | |
Programs response = APIResponse.getInstance(mContext).getPrograms(mSection); | |
if(response==null) | |
result=new ArrayList<Program>(); | |
else | |
result=response.getPrograms(); | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coffeearmy.softtest.rest; | |
import com.coffeearmy.softtest.pojo.Platforms; | |
import com.coffeearmy.softtest.pojo.Programs; | |
import retrofit.Callback; | |
import retrofit.http.GET; | |
import retrofit.http.Path; | |
import retrofit.http.Query; | |
public interface SoftonicAPI { | |
public enum Instance{ | |
es, en, de, fr, it, br, pl, nl, jp, zh, | |
} | |
public enum Format{ | |
xml,json | |
} | |
//http://api2.softonic.com/en/section_getPlatforms/2.json?key=API_KEY&results=2 | |
@GET("/{instance}/section_getPlatforms/2.{format}") | |
void getSection(@Path("instance") Instance instance, | |
@Path("format") Format format, | |
@Query("key") String API_KEY, | |
Callback<Platforms> callback); | |
//http://api2.softonic.com/en/section_getPrograms/2.json?key=API_KEY | |
@GET("/{instance}/section_getPrograms/{section}.{format}") | |
Programs getPrograms(@Path("instance") Instance instance, | |
@Path("section") String section, | |
@Path("format") Format format, | |
@Query("key") String API_KEY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment