package com.ved.musicmapapp.fragments;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.Session;
import com.ved.musicmapapp.LoginAcitivity;
import com.ved.musicmapapp.MainActivity;
import com.ved.musicmapapp.R;
import com.ved.musicmapapp.providers.LogoutTask;
import com.ved.musicmapapp.services.UpdateLocationService;
import com.ved.musicmapapp.utils.SettingsManager;
import com.ved.musicmapapp.utils.Statics;
public class SettingsFragment extends Fragment implements View.OnClickListener {
private SettingsManager mSettingsManager;
private View mBtnAutoUpdateLocationSchedule;
private Activity mContext;
private SharedPreferences prefs;
private Editor edt;
String username;
View logout_fb;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
this.mContext = activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSettingsManager = SettingsManager.getInstance(mContext);
prefs = mContext
.getSharedPreferences("MUSIC_MAP", Context.MODE_PRIVATE);
username = prefs.getString("FB_USERNAME", "");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.activity_settings, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().findViewById(R.id.setting_my_data).setOnClickListener(this);
getView().findViewById(R.id.setting_my_user_like_data)
.setOnClickListener(this);
getView().findViewById(R.id.setting_around_me_radius)
.setOnClickListener(this);
getView().findViewById(R.id.setting_auto_update_location)
.setOnClickListener(this);
getView().findViewById(R.id.setting_notification).setOnClickListener(
this);
getView().findViewById(R.id.setting_about).setOnClickListener(this);
getView().findViewById(R.id.openmylink).setOnClickListener(this);
getView().findViewById(R.id.btnShare).setOnClickListener(this);
TextView tvOpenMyLink = (TextView) getView().findViewById(
R.id.openmylink);
getView().findViewById(R.id.logout_fb).setOnClickListener(this);
tvOpenMyLink.setText("http://playlistr.me/web/" + username);
getView().findViewById(R.id.openlink).setOnClickListener(this);
mBtnAutoUpdateLocationSchedule = getView().findViewById(
R.id.setting_update_location_sequent);
mBtnAutoUpdateLocationSchedule.setOnClickListener(this);
if (mSettingsManager.isAutoUpdateLocation()) {
mBtnAutoUpdateLocationSchedule.setEnabled(true);
} else {
mBtnAutoUpdateLocationSchedule.setEnabled(false);
}
((MainActivity) mContext).setNavTitle("Settings");
}
@Override
public void onDestroyView() {
if (getView() != null) {
((ViewGroup) getView().getParent()).removeView(getView());
}
super.onDestroyView();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.logout_fb:
new LogoutTask(getActivity()).execute(Statics.curUser.getFbid());
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
Intent intent = new Intent(getActivity(), UpdateLocationService.class);
//stopService(intent);
getActivity().finish();
Intent i = new Intent(getActivity(), LoginAcitivity.class);
i.putExtra("LOGOUT", true);
startActivity(i);
break;
case R.id.setting_my_data:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.frame_content,
new PlaylistStatsFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case R.id.setting_my_user_like_data:
FragmentTransaction fragmentTransaction1 = getFragmentManager()
.beginTransaction();
fragmentTransaction1.replace(R.id.frame_content,
new UserLikeDataFragment());
fragmentTransaction1.addToBackStack(null);
fragmentTransaction1.commit();
break;
case R.id.setting_around_me_radius:
onSettingArroundMeRadius();
break;
case R.id.setting_auto_update_location:
onSettingAutoUpdateLocation();
break;
case R.id.setting_update_location_sequent:
onSettingAutoUpdateLocationSchedule();
break;
case R.id.setting_notification:
onSettingNotification();
break;
case R.id.setting_about:
getFragmentManager().beginTransaction()
.replace(R.id.frame_content, new AboutFragment())
.addToBackStack(null).commit();
break;
case R.id.openmylink:
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://playlistr.me/web/" + username));
startActivity(browserIntent);
break;
case R.id.openlink:
Intent browser = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://playlistr.me"));
startActivity(browser);
break;
case R.id.btnShare:
Intent sharingIntent = new Intent(
android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"http://playlistr.me/web/" + username);
startActivity(Intent.createChooser(sharingIntent, "Share"));
break;
default:
break;
}
}
private void onSettingNotification() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
int selectedPosition = mSettingsManager.isShowNotification() ? 0 : 1;
builder.setSingleChoiceItems(R.array.setting_on_off, selectedPosition,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
mSettingsManager.setShowNotification(true);
break;
default:
mSettingsManager.setShowNotification(false);
break;
}
dialog.dismiss();
}
});
builder.create().show();
}
private void onSettingArroundMeRadius() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
int item = 4;
switch (mSettingsManager.getAroundMeRadius()) {
case 50:
item = 0;
break;
case 100:
item = 1;
break;
case 250:
item = 2;
break;
case 500:
item = 3;
break;
default:
item = 4;
break;
}
builder.setSingleChoiceItems(R.array.setting_arround_me_distances,
item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
mSettingsManager.setAroundMeRadius(50);
break;
case 1:
mSettingsManager.setAroundMeRadius(100);
break;
case 2:
mSettingsManager.setAroundMeRadius(250);
break;
case 3:
mSettingsManager.setAroundMeRadius(500);
break;
default:
mSettingsManager.setAroundMeRadius(1000);
break;
}
dialog.dismiss();
}
});
builder.create().show();
}
private void onSettingAutoUpdateLocation() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
int selectedPosition = mSettingsManager.isAutoUpdateLocation() ? 0 : 1;
builder.setSingleChoiceItems(R.array.setting_on_off, selectedPosition,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
mSettingsManager.setAutoUpdateLocation(true);
mBtnAutoUpdateLocationSchedule.setEnabled(true);
break;
default:
mBtnAutoUpdateLocationSchedule.setEnabled(false);
mSettingsManager.setAutoUpdateLocation(false);
break;
}
dialog.dismiss();
}
});
builder.create().show();
}
private void onSettingAutoUpdateLocationSchedule() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
int item = 0;
long time = mSettingsManager.getAutoLocationSchedule();
if(time == 2 * 3600000){
item = 1;
}else if(time == 6 * 3600000){
item = 2;
}else if(time == 12* 3600000){
item = 3;
}else{
item = 0;
}
builder.setSingleChoiceItems(R.array.setting_update_schedule_array,
item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 1:
mSettingsManager
.setAutoLocationSchedule(2 * 3600000);
break;
case 2:
mSettingsManager
.setAutoLocationSchedule(6 * 3600000);
break;
case 3:
mSettingsManager
.setAutoLocationSchedule(12 * 3600000);
break;
default:
mSettingsManager.setAutoLocationSchedule(3600000);
break;
}
dialog.dismiss();
}
});
builder.create().show();
}
}