Advertisement
Guest User

Untitled

a guest
Feb 8th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.62 KB | None | 0 0
  1. package pl.gosell.ghmaster.activity;
  2.  
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5.  
  6. import org.json.JSONException;
  7.  
  8. import pl.gosell.ghmaster.R;
  9. import pl.gosell.ghmaster.api.ApiClient;
  10. import pl.gosell.ghmaster.layout.BottombarLayout;
  11. import pl.gosell.ghmaster.layout.ImageLayout;
  12. import pl.gosell.ghmaster.layout.TopbarLayout;
  13. import pl.gosell.ghmaster.model.ModelSklep;
  14. import android.util.Log;
  15. import android.view.Menu;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.view.View.OnTouchListener;
  20. import android.webkit.JavascriptInterface;
  21. import android.webkit.WebChromeClient;
  22. import android.webkit.WebSettings;
  23. import android.webkit.WebView;
  24. import android.webkit.WebViewClient;
  25. import android.widget.Button;
  26. import android.widget.LinearLayout;
  27. import android.widget.TextView;
  28. import android.widget.Toast;
  29. import android.annotation.SuppressLint;
  30. import android.app.Activity;
  31. import android.content.Context;
  32. import android.content.Intent;
  33. import android.graphics.Bitmap;
  34.  
  35. import com.android.lifter.activity.ActivityBase;
  36. import com.android.lifter.async.AsyncTask;
  37. import com.android.lifter.async.AsyncTask.OnTaskListener;
  38. import com.android.lifter.logging.Loger;
  39.  
  40. public class ActMapa extends ActivityBase
  41. {
  42.     private TopbarLayout llTop = null;
  43.     private BottombarLayout llBottom = null;
  44.    
  45.     private Button btParter = null;
  46.     private Button btPietro1 = null;
  47.     private Button btPietro2 = null;
  48.    
  49.     private WebView wvMapa = null;
  50.    
  51.     private AsyncTask.OnTaskListener taskListener = new AsyncTask.OnTaskListener()
  52.     {
  53.         @Override
  54.         public void onBefore()
  55.         {
  56.         }
  57.  
  58.         @Override
  59.         public void onThread()
  60.         {
  61.             ApiClient apiClient = new ApiClient();
  62.             String apiTxt = "";
  63.            
  64.             try
  65.             {
  66.                 apiTxt = apiClient.mapFloorHtml(1);
  67.                 Log.d("mapa", apiTxt);
  68.                 taskCtrl.result(0, apiTxt);
  69.             }
  70.             catch (MalformedURLException e)
  71.             {
  72.                 e.printStackTrace();
  73.             }
  74.             catch (IOException e)
  75.             {
  76.                 e.printStackTrace();
  77.             }
  78.             catch (JSONException e)
  79.             {
  80.                 e.printStackTrace();
  81.             }
  82.         }
  83.  
  84.         @Override
  85.         public void onFinish()
  86.         {
  87.         }
  88.  
  89.         @Override
  90.         public void onResult(int iWhat, Object obResult)
  91.         {
  92.             //wvMapa.loadData((String) obResult, "text/html; charset=UTF-8", null);
  93.             String data =
  94.                    
  95.                     "<script type=\"text/javascript\"> " +
  96.                     "function openPopup(url) { " +
  97.                     "JavaInf.receiveValueFromJs(url);"+
  98.                     "    return false; " +
  99.                     "} " +
  100.                     "</script>" +
  101.                        
  102.                     "<img src=\"http://www.centrumetc.pl/uploads/floor/1282711124etc_swarzedz_hala1_parter.gif\" href=\"\" usemap=\"#map\" />" +
  103.                     "<map name=\"map\">" +
  104.                         "<area shape=\"poly\" coords=\"695, 305, 695, 348, 663, 348, 663, 305\" alt=\"#1\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\" />" +
  105.                         "<area shape=\"poly\" coords=\"685, 276, 685, 307, 648, 307, 648, 290, 672, 290, 672, 275\" alt=\"#2\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  106.                         "<area shape=\"poly\" coords=\"684, 261, 684, 276, 663, 276, 663, 252, 673, 252, 673, 261\" alt=\"#3\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  107.                         "<area shape=\"poly\" coords=\"662, 253, 676, 253, 676, 260, 686, 260, 686, 242, 660, 242\" alt=\"#4\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  108.                         "<area shape=\"poly\" coords=\"592, 236, 662, 236, 662, 276, 670, 276, 670, 294, 622, 294, 622, 273, 593, 273\" alt=\"#3\" href=\"Sklep1\"onclick=\"return openPopup(this.alt);\"/>" +
  109.                         "<area shape=\"poly\" coords=\"546, 238, 593, 238, 593, 274, 543, 274\" alt=\"#4\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  110.                         "<area shape=\"poly\" coords=\"517, 239, 517, 276, 545, 276, 545, 238\" alt=\"#5\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  111.                         "<area shape=\"poly\" coords=\"417, 343, 484, 343, 487, 354, 495, 360, 505, 362, 520, 359, 525, 351, 528, 345, 664, 345, 664, 305, 648, 305, 648, 292, 622, 292, 622, 274, 357, 274, 357, 326, 371, 326, 371, 311, 383, 311, 383, 317, 417, 317\" onclick=\"return openPopup('Strona');\"/> " +
  112.                         "<area shape=\"poly\" coords=\"383, 318, 416, 318, 416, 376, 382, 376\" alt=\"#6\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  113.                         "<area shape=\"poly\" coords=\"358, 326, 371, 326, 371, 313, 383, 313, 383, 376, 342, 376, 342, 344, 355, 344\" alt=\"#7\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  114.                         "<area shape=\"poly\" coords=\"292, 344, 357, 344, 357, 275, 291, 275\" alt=\"#8\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  115.                         "<area shape=\"poly\" coords=\"186, 341, 291, 341, 291, 275, 226, 275, 226, 298, 216, 298, 216, 320, 188, 320\" alt=\"#9\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  116.                         "<area shape=\"poly\" coords=\"62, 378, 80, 378, 95, 363, 95, 316, 62, 316\" alt=\"#10\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  117.                         "<area shape=\"poly\" coords=\"40, 379, 61, 379, 61, 319, 40, 319\" alt=\"#11\" href=\"Sklep1\" onclick=\"return openPopup('Strona');\"/>" +
  118.                         "<area shape=\"poly\" coords=\"17, 318, 17, 378, 40, 378, 40, 319\" alt=\"#12\" href=\"Sklep1\" onclick=\"return openPopup('Strona');\"/>" +
  119.                         "<area shape=\"poly\" coords=\"93, 303, 20, 306, 20, 319, 92, 319\" alt=\"#13\" href=\"Sklep1\" onclick=\"return openPopup('Strona');\"/>" +
  120.                         "<area shape=\"poly\" coords=\"90, 276, 90, 346, 128, 346, 137, 357, 146, 362, 156, 362, 166, 357, 170, 351, 172, 344, 186, 344, 186, 319, 216, 319, 216, 298, 227, 298, 227, 275\" alt=\"#22\" onclick=\"return openPopup(this.alt);\"/> " +
  121.                         "<area shape=\"poly\" coords=\"93, 137, 93, 276, 169, 276, 169, 135\" alt=\"#14\" href=\"Sklep1\" onclick=\"return openPopup(this.alt);\"/>" +
  122.                         "<area shape=\"poly\" coords=\"684, 208, 520, 208, 520, 240, 688, 240\" alt=\"#15\" onclick=\"return openPopup(this.alt);\"/>" +
  123.                         "<area shape=\"poly\" coords=\"622, 204, 578, 204, 578, 161, 614, 161, 614, 196, 620, 196\" alt=\"#16\" onclick=\"return openPopup(this.alt);\"/>" +
  124.                         "<area shape=\"poly\" coords=\"660, 207, 621, 207, 621, 197, 615, 197, 615, 134, 649, 134, 649, 154, 665, 154\" alt=\"#17\" onclick=\"return openPopup(this.alt);\"/>" +
  125.                         "<area shape=\"poly\" coords=\"683, 174, 683, 210, 663, 210, 663, 175\" alt=\"#18\" onclick=\"return openPopup(this.alt);\"/>" +
  126.                         "<area shape=\"poly\" coords=\"686, 134, 686, 173, 664, 173, 664, 152, 651, 152, 651, 136\" alt=\"#19\" onclick=\"return openPopup(this.alt);\"/>" +
  127.                         "<area shape=\"poly\" coords=\"591, 47, 686, 47, 686, 135, 591, 135\" alt=\"#20\" onclick=\"return openPopup(this.alt);\"/>" +
  128.                         "<area shape=\"poly\" coords=\"96, 45, 592, 45, 592, 136, 615, 136, 615, 162, 578, 162, 578, 210, 521, 210, 521, 275, 169, 275, 169, 136, 93, 136\" alt=\"#21\" onclick=\"return openPopup(this.alt);\"/>" +
  129.                     "</map>" ;
  130.                    
  131.             wvMapa.loadData(data , "text/html; charset=UTF-8", null);      
  132.         }
  133.  
  134.         @Override
  135.         public void onInterrupted()
  136.         {
  137.         }      
  138.     };
  139.  
  140.     private AsyncTask taskCtrl = new AsyncTask(taskListener);
  141.    
  142.    
  143.     @Override
  144.     protected void onRequestFeature()
  145.     {
  146.         removeTitle();
  147.         orientationPortrait();
  148.     }
  149.  
  150.     @Override
  151.     protected int onActivityContentView()
  152.     {
  153.         return R.layout.act_mapa;
  154.     }
  155.  
  156.     @SuppressLint("JavascriptInterface")
  157.     @Override
  158.     protected void onActivityCreate()
  159.     {      
  160.         llTop = (TopbarLayout) findViewById(R.id.tl_top);
  161.         llBottom = (BottombarLayout) findViewById(R.id.bl_bottom);
  162.        
  163.         btParter = super.widgetButton(R.id.bt_parter);
  164.         btPietro1 = super.widgetButton(R.id.bt_pietro_1);
  165.         btPietro2 = super.widgetButton(R.id.bt_pietro_2);
  166.        
  167.         wvMapa = super.widgetWebView(R.id.wv_mapa);
  168.        
  169.         WebSettings webSettings = wvMapa.getSettings();
  170.         webSettings.setJavaScriptEnabled(true);
  171.         webSettings.setBuiltInZoomControls(false);
  172.        
  173.         wvMapa.requestFocusFromTouch();
  174.         wvMapa.requestFocus(View.FOCUS_DOWN);
  175.         wvMapa.addJavascriptInterface(new JavaScriptInterface(this), "JavaInf");
  176.         wvMapa.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  177.        
  178.         llTop.setTytul(getResources().getString(R.string.mapa));
  179.         llTop.setWsteczVisible(false);
  180.         llTop.setSzukajVisible(false);
  181.         llTop.setSpolecznoscVisible(true);
  182.        
  183.         btParter.setPressed(true);
  184.        
  185.         taskCtrl.start();
  186.     }
  187.  
  188.     @Override
  189.     protected void onActivityData(Intent inIntent)
  190.     {
  191.     }
  192.  
  193.     @Override
  194.     protected void onActivityEvents()
  195.     {
  196.         btParter.setOnTouchListener(new OnTouchListener()
  197.         {
  198.             @Override
  199.             public boolean onTouch(View arg0, MotionEvent arg1)
  200.             {
  201.                 btParter.setPressed(true);
  202.                 btPietro1.setPressed(false);
  203.                 btPietro2.setPressed(false);
  204.                
  205.                 return true;
  206.             }          
  207.         });
  208.        
  209.         btPietro1.setOnTouchListener(new OnTouchListener()
  210.         {
  211.             @Override
  212.             public boolean onTouch(View arg0, MotionEvent arg1)
  213.             {
  214.                 btParter.setPressed(false);
  215.                 btPietro1.setPressed(true);
  216.                 btPietro2.setPressed(false);
  217.                
  218.                 return true;
  219.             }          
  220.         });
  221.        
  222.         btPietro2.setOnTouchListener(new OnTouchListener()
  223.         {
  224.             @Override
  225.             public boolean onTouch(View arg0, MotionEvent arg1)
  226.             {
  227.                 btParter.setPressed(false);
  228.                 btPietro1.setPressed(false);
  229.                 btPietro2.setPressed(true);
  230.                
  231.                 return true;
  232.             }          
  233.         });
  234.        
  235.         wvMapa.setWebViewClient(new WebViewClient()
  236.         {
  237.             @Override
  238.             public void onPageStarted(WebView view, String url, Bitmap favicon)
  239.             {
  240.                 Loger.d("page start..." + url);
  241.             }
  242.            
  243.             @Override
  244.             public void onPageFinished(WebView view, String url)
  245.             {
  246.                 Loger.d("page finish..." + url);
  247.             }
  248.            
  249.             @Override
  250.             public void onLoadResource(WebView view, String url)
  251.             {
  252.                 Loger.d("load resource..." + url);
  253.             }
  254.            
  255.             @Override
  256.             public boolean shouldOverrideUrlLoading(WebView view, String url)
  257.             {
  258.                 return true;
  259.             }
  260.         });
  261.        
  262.     }
  263.    
  264.     @Override
  265.     protected void onActivityResume()
  266.     {
  267.     }
  268.  
  269.     @Override
  270.     protected void onActivityOptionsMenu(Menu menu)
  271.     {
  272.     }
  273.  
  274.     @Override
  275.     protected void onActivityOptionsItemSelected(Menu menu)
  276.     {
  277.     }
  278.  
  279.     @Override
  280.     protected void onActivityPause()
  281.     {
  282.     }
  283.  
  284.     @Override
  285.     protected void onActivityDestroy()
  286.     {
  287.     }
  288.  
  289.     @Override
  290.     protected boolean onActivityBackPressed()
  291.     {
  292.         return false;
  293.     }
  294. }
  295.  
  296. class JavaScriptInterface
  297. {
  298.     private Context mContext;
  299.     private static String shopId ="";
  300.     private static ModelSklep shop = null;
  301.  
  302.     private LinearLayout shopBar = null;
  303.     private LinearLayout llBtn = null;
  304.    
  305.     private ImageLayout logo = null;
  306.     private TextView nazwa = null;
  307.    
  308.     private Button btnSchowaj = null;
  309.    
  310.     private OnTaskListener getShop = new OnTaskListener() {
  311.        
  312.         private static final int RES_OK = 0;
  313.         private static final int RES_EMPTY = 1;
  314.        
  315.         private static final int RES_ERROR_URL = -1;
  316.         private static final int RES_ERROR_IO  = -2;
  317.         private static final int RES_ERROR_JSON = -3;
  318.        
  319.         @Override
  320.         public void onThread()
  321.         {
  322.             ApiClient client = new ApiClient();
  323.            
  324.             if(shopId.length() > 0)
  325.             {
  326.                 try
  327.                 {
  328.                     shop = client.getSklep(shopId);
  329.                    
  330.                     if(shop != null)
  331.                     {
  332.                         getShopCtrl.result(RES_OK, shop);
  333.                     }
  334.                     else
  335.                     {
  336.                         getShopCtrl.result(RES_EMPTY, shop);
  337.                     }
  338.                 }
  339.                 catch (MalformedURLException e)
  340.                 {
  341.                     e.printStackTrace();
  342.                     getShopCtrl.result(RES_ERROR_URL, null);
  343.                 }
  344.                 catch (IOException e)
  345.                 {
  346.                     e.printStackTrace();
  347.                     getShopCtrl.result(RES_ERROR_IO, null);
  348.                 }
  349.                 catch (JSONException e)
  350.                 {
  351.                     e.printStackTrace();
  352.                     getShopCtrl.result(RES_ERROR_JSON, null);
  353.                 }  
  354.             }
  355.         }
  356.        
  357.         @Override
  358.         public void onResult(int iWhat, Object obResult)
  359.         {
  360.             if(iWhat == RES_OK)
  361.             {
  362.                 if(((ModelSklep)obResult).logoUrl.length() > 0)
  363.                 {
  364.                     logo.imageFromWeb(((ModelSklep)obResult).logoUrl);
  365.                 }
  366.                 else
  367.                 {
  368.                     logo.imageFromResource(R.drawable.ico_aplikacja);
  369.                 }
  370.  
  371.                 nazwa.setText(((ModelSklep)obResult).nazwaPelna);
  372.             }
  373.             else
  374.             {
  375.                 logo.imageFromResource(R.drawable.ico_aplikacja);
  376.                 nazwa.setText(mContext.getResources().getString(R.string.noShop));
  377.             }
  378.         }
  379.        
  380.         @Override
  381.         public void onInterrupted()
  382.         {  
  383.         }
  384.        
  385.         @Override
  386.         public void onFinish()
  387.         {
  388.         }
  389.        
  390.         @Override
  391.         public void onBefore()
  392.         {
  393.             if(shopBar.getVisibility() == View.GONE)
  394.             {
  395.                 ((Activity) mContext).runOnUiThread(new Runnable()
  396.                 {
  397.    
  398.                     @Override
  399.                     public void run()
  400.                     {
  401.                         shopBar.setVisibility(View.VISIBLE);
  402.                     }
  403.                    
  404.                 });
  405.             }
  406.         }
  407.     };
  408.    
  409.     private AsyncTask getShopCtrl = new AsyncTask(getShop);
  410.    
  411.    
  412.     public JavaScriptInterface(Context c)
  413.     {
  414.         mContext = c;
  415.        
  416.         logo = (ImageLayout) ((Activity) c).findViewById(R.id.logo);
  417.         nazwa = (TextView) ((Activity) c).findViewById(R.id.shopTitle);
  418.         shopBar = (LinearLayout) ((Activity) c).findViewById(R.id.shop_bar);
  419.  
  420.         btnSchowaj = ((ActivityBase) c).widgetButton(R.id.schowaj);
  421.        
  422.         btnSchowaj.setOnTouchListener(new OnTouchListener()
  423.         {
  424.            
  425.             @Override
  426.             public boolean onTouch(View v, MotionEvent event)
  427.             {
  428.                 if(event.getAction() == MotionEvent.ACTION_UP)
  429.                 {
  430.                     shopBar.setVisibility(View.GONE);
  431.                     nazwa.setText("");
  432.                 }
  433.                
  434.                 return false;
  435.             }
  436.         });
  437.                
  438.  
  439.     }
  440.    
  441.     @JavascriptInterface
  442.     public void receiveValueFromJs(String str)
  443.     {
  444.         shopId = "";
  445.        
  446.         for(int i = 1 ; i < str.length() ; i++)
  447.         {
  448.             shopId += str.charAt(i);
  449.         }
  450.         getShopCtrl.start();
  451.         //Toast.makeText(mContext, "Value from JS: " + shopId,Toast.LENGTH_SHORT).show();
  452.     }
  453.    
  454.     public static String getId()
  455.     {
  456.         return shopId;
  457.     }
  458.    
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement