1212import android .graphics .Rect ;
1313import android .net .Uri ;
1414import android .os .Bundle ;
15+ import android .os .PersistableBundle ;
1516import android .support .annotation .NonNull ;
1617import android .support .v4 .content .FileProvider ;
1718import android .support .v7 .app .AppCompatActivity ;
19+ import android .text .TextUtils ;
1820import android .view .Menu ;
1921import android .view .MenuItem ;
2022import android .view .View ;
2123import android .view .ViewTreeObserver ;
2224import android .widget .TextView ;
2325import android .widget .Toast ;
2426
27+ import org .w3c .dom .Text ;
28+
2529import java .io .ByteArrayOutputStream ;
2630import java .io .File ;
2731import java .io .FileOutputStream ;
2832import java .io .IOException ;
33+ import java .util .List ;
2934
3035import fr .tvbarthel .apps .cameracolorpicker .R ;
3136import fr .tvbarthel .apps .cameracolorpicker .data .ColorItem ;
3237import fr .tvbarthel .apps .cameracolorpicker .data .ColorItems ;
38+ import fr .tvbarthel .apps .cameracolorpicker .data .Palette ;
39+ import fr .tvbarthel .apps .cameracolorpicker .data .Palettes ;
3340import fr .tvbarthel .apps .cameracolorpicker .fragments .DeleteColorDialogFragment ;
41+ import fr .tvbarthel .apps .cameracolorpicker .fragments .EditTextDialogFragment ;
3442import fr .tvbarthel .apps .cameracolorpicker .utils .ClipDatas ;
3543
36- public class ColorDetailActivity extends AppCompatActivity implements View .OnClickListener , DeleteColorDialogFragment .Callback {
44+ public class ColorDetailActivity extends AppCompatActivity implements View .OnClickListener ,
45+ DeleteColorDialogFragment .Callback , EditTextDialogFragment .Callback {
3746
3847 /**
3948 * A key for passing a color item as extra.
@@ -46,11 +55,10 @@ public class ColorDetailActivity extends AppCompatActivity implements View.OnCli
4655 private static final String EXTRA_START_BOUNDS = "ColorDetailActivity.Extras.EXTRA_START_BOUNDS" ;
4756
4857 /**
49- * A key for knowing if the {@link ColorItem} can be deleted.
50- * <p/>
51- * If the color item can not be deleted, the delete action will be removed from the menu.
58+ * A key for passing an optional palette that is associated with the color item displayed.
59+ *
5260 */
53- private static final String EXTRA_CAN_BE_DELETED = "ColorDetailActivity.Extras.EXTRA_CAN_BE_DELETED " ;
61+ private static final String EXTRA_PALETTE = "ColorDetailActivity.Extras.EXTRA_PALETTE " ;
5462
5563 /**
5664 * The quality of the image compressed before sharing.
@@ -77,16 +85,26 @@ public class ColorDetailActivity extends AppCompatActivity implements View.OnCli
7785 */
7886 private static final String FILE_PROVIDER_AUTHORITY = "fr.tvbarthel.apps.cameracolorpicker.fileprovider" ;
7987
80- public static void startWithColorItem (Context context , ColorItem colorItem , View colorPreviewClicked ,
81- boolean canBeDeleted ) {
88+ /**
89+ * A request code to use in {@link EditTextDialogFragment#newInstance(int, int, int, int, String, String, boolean)}.
90+ */
91+ private static final int REQUEST_CODE_EDIT_COLOR_ITEM_NAME = 15 ;
92+
93+ public static void startWithColorItem (Context context , ColorItem colorItem ,
94+ View colorPreviewClicked ){
95+ startWithColorItem (context , colorItem , colorPreviewClicked , null );
96+ }
97+
98+ public static void startWithColorItem (Context context , ColorItem colorItem ,
99+ View colorPreviewClicked , Palette palette ) {
82100 final boolean isActivity = context instanceof Activity ;
83101 final Rect startBounds = new Rect ();
84102 colorPreviewClicked .getGlobalVisibleRect (startBounds );
85103
86104 final Intent intent = new Intent (context , ColorDetailActivity .class );
87105 intent .putExtra (EXTRA_COLOR_ITEM , colorItem );
88106 intent .putExtra (EXTRA_START_BOUNDS , startBounds );
89- intent .putExtra (EXTRA_CAN_BE_DELETED , canBeDeleted );
107+ intent .putExtra (EXTRA_PALETTE , palette );
90108
91109 if (!isActivity ) {
92110 intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
@@ -137,11 +155,9 @@ public static void startWithColorItem(Context context, ColorItem colorItem, View
137155 private ColorItem mColorItem ;
138156
139157 /**
140- * A boolean for knowing if the {@link ColorItem} can be deleted or not.
141- * <p/>
142- * If false, the delete action will be removed from the menu.
158+ * An optional {@link Palette} that is associated with the {@link ColorItem}.
143159 */
144- private boolean mCanBeDeleted ;
160+ private Palette mPalette ;
145161
146162 @ Override
147163 protected void onCreate (Bundle savedInstanceState ) {
@@ -152,14 +168,26 @@ protected void onCreate(Bundle savedInstanceState) {
152168 // ensure correct extras.
153169 final Intent intent = getIntent ();
154170 if (!intent .hasExtra (EXTRA_COLOR_ITEM ) || !intent .hasExtra (EXTRA_START_BOUNDS )
155- || !intent .hasExtra (EXTRA_CAN_BE_DELETED )) {
171+ || !intent .hasExtra (EXTRA_PALETTE )) {
156172 throw new IllegalStateException ("Missing extras. Please use startWithColorItem." );
157173 }
158174
159175 // Retrieve the extras.
160- mColorItem = intent .getParcelableExtra (EXTRA_COLOR_ITEM );
161176 final Rect startBounds = intent .getParcelableExtra (EXTRA_START_BOUNDS );
162- mCanBeDeleted = intent .getBooleanExtra (EXTRA_CAN_BE_DELETED , true );
177+ if (savedInstanceState == null ) {
178+ mColorItem = intent .getParcelableExtra (EXTRA_COLOR_ITEM );
179+ mPalette = intent .getParcelableExtra (EXTRA_PALETTE );
180+ } else {
181+ mColorItem = savedInstanceState .getParcelable (EXTRA_COLOR_ITEM );
182+ mPalette = savedInstanceState .getParcelable (EXTRA_PALETTE );
183+ }
184+
185+ // Set the title of the activity with the name of the color, if not null.
186+ if (!TextUtils .isEmpty (mColorItem .getName ())) {
187+ setTitle (mColorItem .getName ());
188+ } else {
189+ setTitle (mColorItem .getHexString ());
190+ }
163191
164192 // Create a rect that will be used to retrieve the stop bounds.
165193 final Rect stopBounds = new Rect ();
@@ -241,11 +269,19 @@ protected void onPause() {
241269 hideToast ();
242270 }
243271
272+ @ Override
273+ public void onSaveInstanceState (Bundle outState ) {
274+ super .onSaveInstanceState (outState );
275+ outState .putParcelable (EXTRA_COLOR_ITEM , mColorItem );
276+ outState .putParcelable (EXTRA_PALETTE , mPalette );
277+ }
278+
244279 @ Override
245280 public boolean onCreateOptionsMenu (Menu menu ) {
246281 // Inflate the menu; this adds items to the action bar if it is present.
247282 getMenuInflater ().inflate (R .menu .menu_color_detail , menu );
248- if (!mCanBeDeleted ) {
283+ if (mPalette != null ) {
284+ // A color associated with a palette can't be deleted.
249285 menu .removeItem (R .id .menu_color_detail_action_delete );
250286 }
251287 return true ;
@@ -266,6 +302,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
266302 finish ();
267303 } else if (id == R .id .menu_color_detail_action_share ) {
268304 return handleActionShare ();
305+ } else if (id == R .id .menu_color_detail_action_edit ) {
306+ EditTextDialogFragment .newInstance (REQUEST_CODE_EDIT_COLOR_ITEM_NAME ,
307+ R .string .activity_color_detail_edit_text_dialog_fragment_title ,
308+ R .string .activity_color_detail_edit_text_dialog_fragment_positive_button ,
309+ android .R .string .cancel ,
310+ mColorItem .getHexString (),
311+ mColorItem .getName (), true ).show (getSupportFragmentManager (), null );
269312 }
270313
271314 return super .onOptionsItemSelected (item );
@@ -300,6 +343,45 @@ public void onColorDeletionConfirmed(@NonNull ColorItem colorItemToDelete) {
300343 }
301344 }
302345
346+ @ Override
347+ public void onEditTextDialogFragmentPositiveButtonClick (int requestCode , String text ) {
348+ if (requestCode == REQUEST_CODE_EDIT_COLOR_ITEM_NAME ) {
349+ // Update the title of the activity.
350+ if (TextUtils .isEmpty (text )) {
351+ setTitle (mColorItem .getHexString ());
352+ } else {
353+ setTitle (text );
354+ }
355+
356+ // Set the new name.
357+ mColorItem .setName (text );
358+
359+ // Persist the change.
360+ if (mPalette == null ) {
361+ // The color item is a standalone color.
362+ // It's not associated with a palette.
363+ // Just save the color item.
364+ ColorItems .saveColorItem (this , mColorItem );
365+ } else {
366+ // The color item is associated with a palette.
367+ // Edit and save the palette.
368+ final List <ColorItem > colorItems = mPalette .getColors ();
369+ for (ColorItem candidate : colorItems ) {
370+ if (candidate .getId () == mColorItem .getId ()) {
371+ candidate .setName (text );
372+ break ;
373+ }
374+ }
375+ Palettes .saveColorPalette (this , mPalette );
376+ }
377+ }
378+ }
379+
380+ @ Override
381+ public void onEditTextDialogFragmentNegativeButtonClick (int requestCode ) {
382+ // nothing to do here.
383+ }
384+
303385
304386 protected void clipColor (int labelResourceId , CharSequence colorString ) {
305387 ClipDatas .clipPainText (this , getString (labelResourceId ), colorString );
@@ -379,5 +461,4 @@ private boolean handleActionShare() {
379461
380462 return handled ;
381463 }
382-
383464}
0 commit comments