Skip to content

Commit

Permalink
Add a copy button for coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Trewartha committed Jul 7, 2017
1 parent 0950526 commit 9027cf1
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.trewartha.positional.position;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -20,8 +19,8 @@ public class CoordinatesDegreesDecimalFragment extends CoordinatesFragment {
private static final String FORMAT_LONGITUDE = "% 10.5f";
private static final Locale LOCALE = Locale.getDefault();

@BindView(R.id.degrees_decimal_latitude_text_view) @Nullable TextView latitudeTextView;
@BindView(R.id.degrees_decimal_longitude_text_view) @Nullable TextView longitudeTextView;
@BindView(R.id.degrees_decimal_latitude_text_view) TextView latitudeTextView;
@BindView(R.id.degrees_decimal_longitude_text_view) TextView longitudeTextView;

private Unbinder unbinder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.trewartha.positional.position;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
Expand All @@ -20,6 +23,7 @@

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -46,6 +50,7 @@ public class PositionFragment extends LocationAwareFragment implements CompoundB

@BindView(R.id.progress_bar) ProgressBar progressBar;
@BindView(R.id.screen_lock_switch) ImageView screenLockSwitch;
@BindView(R.id.copy_button) ImageView copyButton;

private List<CoordinatesFragment> coordinatesFragments;
private CoordinatesFormat coordinatesFormat;
Expand Down Expand Up @@ -121,6 +126,37 @@ public void onLocationChanged(@Nullable Location location) {
}
}

@OnClick(R.id.copy_button)
public void onCopyClicked() {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getContext());
bottomSheetDialog.setCanceledOnTouchOutside(true);
bottomSheetDialog.setCancelable(true);
bottomSheetDialog.setTitle(R.string.settings_copy_coordinates_title);
bottomSheetDialog.setContentView(R.layout.coordinates_copy_fragment);

CoordinatesCopier coordinatesCopier = new CoordinatesCopier(new OnCoordinatesCopiedListener() {
@Override
public void onCopy() {
bottomSheetDialog.dismiss();
}
});

View bothTextView = bottomSheetDialog.findViewById(R.id.coordinates_copy_both_text_view);
View latitudeTextView = bottomSheetDialog.findViewById(R.id.coordinates_copy_latitude_text_view);
View longitudeTextView = bottomSheetDialog.findViewById(R.id.coordinates_copy_longitude_text_view);

if (bothTextView == null || latitudeTextView == null || longitudeTextView == null) {
Toast.makeText(getContext(), R.string.copied_coordinates_failure, Toast.LENGTH_SHORT).show();
return;
}

bothTextView.setOnClickListener(coordinatesCopier);
latitudeTextView.setOnClickListener(coordinatesCopier);
longitudeTextView.setOnClickListener(coordinatesCopier);

bottomSheetDialog.show();
}

@OnClick({R.id.elevation_unit_text_view, R.id.speed_unit_text_view, R.id.accuracy_unit_text_view})
public void onDistanceUnitClicked() {
useMetricUnits = !useMetricUnits;
Expand Down Expand Up @@ -247,4 +283,54 @@ public void onPageScrollStateChanged(int state) {
// Don't do anything here
}
}

private class CoordinatesCopier implements View.OnClickListener {

private OnCoordinatesCopiedListener onCoordinatesCopiedListener;

CoordinatesCopier(@Nullable OnCoordinatesCopiedListener onCoordinatesCopiedListener) {
this.onCoordinatesCopiedListener = onCoordinatesCopiedListener;
}

@Override
public void onClick(View v) {
if (location == null) {
Toast.makeText(getContext(), R.string.copied_coordinates_failure, Toast.LENGTH_SHORT).show();
return;
}

ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
String clipDataLabel = getString(R.string.copied_coordinates_label);
String clipDataText = "";
String toastText = "";

switch (v.getId()) {
case R.id.coordinates_copy_both_text_view:
clipDataText = String.format(Locale.US, "%f, %f", location.getLatitude(), location.getLongitude());
toastText = getString(R.string.copied_coordinates_both_success);
break;
case R.id.coordinates_copy_latitude_text_view:
clipDataText = String.format(Locale.US, "%f", location.getLatitude());
toastText = getString(R.string.copied_coordinates_latitude_success);
break;
case R.id.coordinates_copy_longitude_text_view:
clipDataText = String.format(Locale.US, "%f", location.getLongitude());
toastText = getString(R.string.copied_coordinates_longitude_success);
break;
}

ClipData coordinatesClipData = ClipData.newPlainText(clipDataLabel, clipDataText);
clipboardManager.setPrimaryClip(coordinatesClipData);

Toast.makeText(getContext(), toastText, Toast.LENGTH_SHORT).show();

if (onCoordinatesCopiedListener != null) {
onCoordinatesCopiedListener.onCopy();
}
}
}

private interface OnCoordinatesCopiedListener {
void onCopy();
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_content_copy_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z"/>
</vector>
27 changes: 21 additions & 6 deletions app/src/main/res/layout-land/position_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,44 @@

</android.support.v4.view.ViewPager>

<ImageView
android:id="@+id/screen_lock_switch"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:text="@string/settings_screen_lock_content_description"
android:tint="@color/screenLockButtonTint"
android:translationZ="1dp"
tools:targetApi="lollipop"/>

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_margin="16dp"
android:layout_toEndOf="@id/screen_lock_switch"
android:layout_toRightOf="@id/screen_lock_switch"
android:indeterminateOnly="true"/>

<ImageView
android:id="@+id/screen_lock_switch"
android:id="@+id/copy_button"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_label"
android:contentDescription="@string/settings_copy_coordinates_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:text="@string/settings_screen_lock_label"
android:src="@drawable/ic_content_copy_black_24dp"
android:tint="@color/screenLockButtonTint"
android:translationZ="1dp"
tools:targetApi="lollipop"/>
Expand Down
28 changes: 21 additions & 7 deletions app/src/main/res/layout-large-land/position_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,44 @@

</android.support.v4.view.ViewPager>

<ImageView
android:id="@+id/screen_lock_switch"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:tint="@color/screen_lock_selector"
android:translationZ="1dp"
tools:targetApi="lollipop"/>

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_margin="16dp"
android:layout_toEndOf="@id/screen_lock_switch"
android:layout_toRightOf="@id/screen_lock_switch"
android:indeterminateOnly="true"/>

<ImageView
android:id="@+id/screen_lock_switch"
android:id="@+id/copy_button"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_label"
android:contentDescription="@string/settings_copy_coordinates_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:text="@string/settings_screen_lock_label"
android:tint="@color/screen_lock_selector"
android:src="@drawable/ic_content_copy_black_24dp"
android:tint="@color/screenLockButtonTint"
android:translationZ="1dp"
tools:targetApi="lollipop"/>

Expand Down
30 changes: 22 additions & 8 deletions app/src/main/res/layout-large/position_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,44 @@

</android.support.v4.view.ViewPager>

<ImageView
android:id="@+id/screen_lock_switch"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:tint="@color/screen_lock_selector"
android:translationZ="1dp"
tools:targetApi="lollipop"/>

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toRightOf="@id/screen_lock_switch"
android:layout_toEndOf="@id/screen_lock_switch"
android:layout_gravity="center"
android:layout_margin="16dp"
android:indeterminateOnly="true"/>

<ImageView
android:id="@+id/screen_lock_switch"
android:id="@+id/copy_button"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_label"
android:contentDescription="@string/settings_copy_coordinates_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:text="@string/settings_screen_lock_label"
android:tint="@color/screen_lock_selector"
android:src="@drawable/ic_content_copy_black_24dp"
android:tint="@color/screenLockButtonTint"
android:translationZ="1dp"
tools:targetApi="lollipop"/>

Expand Down
31 changes: 31 additions & 0 deletions app/src/main/res/layout/coordinates_copy_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bottomSheetBackground"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
android:id="@+id/coordinates_copy_title_text_view"
style="@style/BottomSheetTitle"
android:text="@string/settings_copy_coordinates_title"/>

<TextView
android:id="@+id/coordinates_copy_both_text_view"
style="@style/BottomSheetListItem"
android:text="@string/settings_copy_coordinates_both"/>

<TextView
android:id="@+id/coordinates_copy_latitude_text_view"
style="@style/BottomSheetListItem"
android:text="@string/settings_copy_coordinates_latitude"/>

<TextView
android:id="@+id/coordinates_copy_longitude_text_view"
style="@style/BottomSheetListItem"
android:text="@string/settings_copy_coordinates_longitude"/>

</LinearLayout>
28 changes: 21 additions & 7 deletions app/src/main/res/layout/position_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,43 @@

</android.support.v4.view.ViewPager>

<ImageView
android:id="@+id/screen_lock_switch"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:tint="@color/screenLockButtonTint"
android:translationZ="1dp"
tools:targetApi="lollipop"/>

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toRightOf="@id/screen_lock_switch"
android:layout_toEndOf="@id/screen_lock_switch"
android:layout_gravity="center"
android:layout_margin="16dp"
android:indeterminateOnly="true"/>

<ImageView
android:id="@+id/screen_lock_switch"
android:id="@+id/copy_button"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/settings_screen_lock_label"
android:contentDescription="@string/settings_copy_coordinates_content_description"
android:padding="16dp"
android:src="@drawable/ic_screen_lock_portrait_black_24dp"
android:text="@string/settings_screen_lock_label"
android:src="@drawable/ic_content_copy_black_24dp"
android:tint="@color/screenLockButtonTint"
android:translationZ="1dp"
tools:targetApi="lollipop"/>
Expand Down
Loading

0 comments on commit 9027cf1

Please sign in to comment.