/*

CLASS {NATIVE_Clipboard|_Android}, NATIVE:

*/
    import android.content.ClipboardManager;
    import android.content.Context;
    import android.content.ClipData;
    import java.util.concurrent.*;

    public class NATIVE_Clipboard
    {
    
        public static byte Last_foreground_color = 7;
        public static byte Last_background_color = 0;

		public static String Get_text_from_clipboard() throws Exception
		{
		    Callable<String> callable = new Callable<String>() {
		        @Override
		        public String call() throws Exception {
		     	   try {
		     		   ClipboardManager CM = (ClipboardManager)
		     			   AL4_form.context_activity
		     			   .getSystemService(Context.CLIPBOARD_SERVICE);
		     		   if (CM != null && CM.hasPrimaryClip()) {
		     			   ClipData CD = CM.getPrimaryClip();
		     			   if (CD != null) {
		     				   ClipData.Item item = CD.getItemAt(0);
		     	 			   String text = item.getText().toString();
		     	 			   if (text != null) {
		     					   return text;
		     				   }
		     			   }
		     		   }
		     	   }
		     	   catch (Exception ex2) {
		      		   AL4_system.WriteLine(
		      		   "exception in Text_from_clipboard/2: " + ex2);
		     	   }
		      	   return "";
		        }
		    };
		    FutureTask<String> task = new FutureTask<>(callable);
		    AL4_form.context_activity.runOnUiThread(task);
		    return task.get();
		}
		
		public static void Set_text_to_clipboard(String X_Text)
		{
		    try {
	     	   AL4_form.context_activity.runOnUiThread(new Runnable(){
	     		   public void run() {
	     			   try {
	     				   ClipboardManager CM = (ClipboardManager)
	     					   AL4_form.context_activity
	     					   .getSystemService(Context.CLIPBOARD_SERVICE);
	      				   ClipData CD = ClipData.newPlainText("text", X_Text);
	     				   CM.setPrimaryClip( CD );
	     			   }
	     			   catch (Exception ex2) {
	      				   AL4_system.WriteLine(
	      				   "exception in Text_to_clipboard/2: " + ex2);
	     			   }
	     		   }
	     	   });
	        }
	        catch (Exception ex) {
	      	   AL4_system.WriteLine("exception in Text_to_clipboard: " + ex);
	        }
		}
		
		public static boolean Is_text_in_clipboard() throws Exception
		{
		    Callable<Boolean> callable = new Callable<Boolean>() {
		        @Override
		        public Boolean call() throws Exception {
		     	   try {
		     		   ClipboardManager CM = (ClipboardManager)
		     			   AL4_form.context_activity
		     			   .getSystemService(Context.CLIPBOARD_SERVICE);
		     		   if (CM != null && CM.hasPrimaryClip()) {
		     			   ClipData CD = CM.getPrimaryClip();
		     			   if (CD != null) {
		     				   ClipData.Item item = CD.getItemAt(0);
		     	 			   String text = item.getText().toString();
		     	 			   if (text != null) {
		     					   return true;
		     				   }
		     			   }
		     		   }
		     	   }
		     	   catch (Exception ex2) {
		      		   AL4_system.WriteLine(
		      		   "exception in Text_from_clipboard/2: " + ex2);
		     	   }
		      	   return false;
		        }
		    };
		    FutureTask<Boolean> task = new FutureTask<>(callable);
		    AL4_form.context_activity.runOnUiThread(task);
		    return task.get();
		}

		public static byte Get_files_from_clipboard(DynAStr X_Paths)
			throws Exception
		{
			X_Paths.Clear();
		    Callable<String> callable = new Callable<String>() {
		        @Override
		        public String call() throws Exception {
		     	   try {
		     		   ClipboardManager CM = (ClipboardManager)
		     			   AL4_form.context_activity
		     			   .getSystemService(Context.CLIPBOARD_SERVICE);
		     		   if (CM != null && CM.hasPrimaryClip()) {
		     			   ClipData CD = CM.getPrimaryClip();
		     			   if (CD != null) {
		     			   	   String text = "";
		     			   	   int count = CD.getItemCount();
		     			   	   for (int i = 0; i < count; i++)
		     			   	   {
			     				   ClipData.Item item = CD.getItemAt(i);
			     				   Object u = item.getUri();
			     				   if (u == null) continue;
			     				   text += u.toString() + '\0';
		     			   	   }
		     			   }
		     		   }
		     	   }
		     	   catch (Exception ex2) {
		      		   AL4_system.WriteLine(
		      		   "exception in Text_from_clipboard/2: " + ex2);
		     	   }
		      	   return "";
		        }
		    };
		    FutureTask<String> task = new FutureTask<>(callable);
		    AL4_form.context_activity.runOnUiThread(task);
		    String all = task.get();
		    if (all == "") return 0;
		    String[] spl = all.split("\0");
		    for (int i = 0; i < spl.length; i++) {
		    	X_Paths.Add(spl[i]);	
		    }
		    return 0;
		}        
		
    	public static void Files_to_clipboard(FixAStr Paths, byte Op)
    	{
    	}
    	
		
    	public static boolean Clipboard_has_files()
    	{
    		return false;
    	}
    	
		
        
        
/*


CONST STR:
    NATIVE_Import_clipboard = ""

    NATIVE_Text_from_clipboard = @
    "RESULT = NATIVE_Clipboard.Get_text_from_clipboard();"

    NATIVE_Text_to_clipboard = @
    "NATIVE_Clipboard.Set_text_to_clipboard(X_Text);"
    
    NATIVE_Clipboard_has_text = @
    "RESULT = NATIVE_Clipboard.Is_text_in_clipboard();"
    
    
    NATIVE_Files_from_clipboard = @
    "RESULT = NATIVE_Clipboard.Get_files_from_clipboard(X_Paths);"
    
    NATIVE_Files_to_clipboard = @
    "NATIVE_Clipboard.Files_to_clipboard(X_Paths, X_Op);"
    
    NATIVE_Clipboard_has_files = @
    "RESULT = NATIVE_Clipboard.Clipboard_has_files();"
    .
        
END
*/
	}

