
    %j                         d Z ddlZddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
Z
ddlmZ ddlmZmZ ddlmZ  ej$                  e      Z G d d	e      Zy)
z3Created on Sun Apr 07 2024 20:07:44 by codeskyblue
    N)Path)OptionalUnion)AbstractDevice)ReadProgresshumanize)AdbInstallErrorc                       e Zd Zededefd       Zddddddgfdeeef   d	ed
edede	j                  egdf   defdZddddddgdfdeeef   d	ed
edede	j                  egdf   dedefdZddededefdZy)InstallExtensionurlpathc                 p   	 t        j                  | d      }|j                          t        |d      5 }|j	                  d      D ]  }|s|j                  |        	 ddd       y# 1 sw Y   yxY w# t         j                  j                  $ r!}t        j                  d|  d|         d}~ww xY w)	a#  
        Download apk file from url
    
        Args:
            url (str): The URL of the APK file to download.
            path (Path): The local file path where the APK will be saved.
    
        Raises:
            requests.exceptions.RequestException: If the download fails.
        T)streamwbi (  )
chunk_sizeNzFailed to download APK from z: )
requestsgetraise_for_statusopeniter_contentwrite
exceptionsRequestExceptionloggerwarning)r   r   responsefilechunkes         T/var/www/oz-panel.ru/ozpay-pub/venv/lib/python3.12/site-packages/adbutils/install.pydownload_apkzInstallExtension.download_apk   s    	||C5H%%' dD! *T%22e2D *E

5)** * * ""33 	NN9#bDE	s:   3A7 A+A+"A7 +A40A7 4A7 7B5B00B5FNz-rz-tpath_or_urlnolaunch	uninstallsilentcallbackflagsc           	          	 dd l }d}| j	                  |||||||       y # t        $ r t        j                  d       d}Y :w xY w)Nr   TzFapkutils is not installed, install it with 'pip install adbutils[apk]'F)apkutilsImportErrorr   r   _install)	selfr"   r#   r$   r%   r&   r'   r)   has_apkutilss	            r    installzInstallExtension.install3   sM    	!L 	k8Y%Q]^  	!NNcd L	!s      AATr-   c                    fd}t        |t              rt        j                  d|      rlt	        j
                  d      }	|	j                          | j                  |t        |	j                               t        |	j                        }
 |d|
        nt        |      }
|
j                         st        d|       d}d}|rddl}|j                  j                  t        |
            5 }|j                          |j!                         }|j"                  }|r|d   nd}|r|j%                  d	      d
k(  rd	|z   }ddd        |d|         |d|        d|xs d d} |d|        | j'                  |
|        | j(                  j+                  |      }|
j+                         j,                  }|j.                  |k(  st1        d| d|j.                          |r|r |d|        | j3                  |        |d       	 t5        j4                         }|r |d       | j7                  |d|       t5        j4                         |z
  } |d|dd       |s$|r"|r  |d|d|       | j9                  ||       |r	 |d%       yy# 1 sw Y   ZxY w# t0        $ r}|r|j:                  dv r |d|d|j:                         | j3                  |       | j7                  |d|        |dt5        j4                         z
   d       |sH|rF |d | d|        | j9                  ||       n%t=        d!|j:                  z   d"z   d#z   d$z   |z           Y d}~d}~ww xY w# |r	 |d%       w w xY w)&a  
        Install APK to device

        Args:
            path_or_url: local path or http url
            nolaunch: do not launch app after install
            uninstall: uninstall app before install
            silent: disable log message print
            callback: only two event now: <"BEFORE_INSTALL" | "FINALLY">
            flags (list): default ["-r", "-t"]
            has_apkutils: whether apkutils is installed

        Raises:
            AdbInstallError, BrokenPipeError
        c                 "    st        |        y y )N)print)msgr%   s    r    dprintz)InstallExtension._install.<locals>.dprintY   s    c
     z
^https?://z.apk)suffixzdownload apk to zFile or URL not found: Nr   .zAPK packageName: zAPK mainActivity: z/data/local/tmp/unknownzpush apk to device: )show_progressz$pushed apk size not matched, expect z got zuninstall app: zinstall to android system ...BEFORE_INSTALLT)cleanr'   z"successfully installed, time used z.1fz secondszlaunch app: /))INSTALL_FAILED_PERMISSION_MODEL_DOWNGRADE"INSTALL_FAILED_UPDATE_INCOMPATIBLE INSTALL_FAILED_VERSION_DOWNGRADEz
uninstall z	 because zLaunch app: zFailure 
z6Remote apk is not removed. Manually install command:
	zadb shell pm install -r -t FINALLY)
isinstancestrrematchtempfileNamedTemporaryFilecloser!   r   nameis_fileFileNotFoundErrorr)   APK	from_fileget_manifestget_manifest_main_activitiespackage_namefind	_push_apksyncstatst_sizesizer	   r$   timeinstall_remote	app_startreasonr1   )r,   r"   r#   r$   r%   r&   r'   r-   r3   tmpfilesrc_pathrP   main_activityr)   apk
activities
device_dstinfoapk_sizestart	time_usedr   s       `                 r    r+   zInstallExtension._installB   sq   .	 k3'BHH]K,P11@GMMOk4+=>GLL)H%hZ01K(H!#&=k]$KLL''H6 8#  " ==?
"//1;
1 ]%7%7%<%B$'-$7M8 &|n56'78'(A	'B$G
%j\23xv:Fyy~~j)==?**yyH$B8*ERVR[R[Q\]^_\N34NN<(./"	$IIKE)*
$eD		e+I7	#hOPlMJK|];. # w8 8L  	 - !
 L!((KL|,##Jd%#H;DIIK%<O;PPXYZM\,qHINN<? )D0NO346@AB '	* # s9   0AJA<J& J#&	M1/B8M,'M4 ,M11M4 4Napk_pathr`   r9   c                    t        j                          }|j                         j                  }|j                  d      5 }|r4t	        ||t        |            }| j                  j                  ||       n| j                  j                  ||       ddd       t        j                  dt        j                          |z
  z         y# 1 sw Y   7xY w)z
        Push APK file to device with progress indication.
    
        Args:
            apk_path (Path): Path to the APK file.
            device_dst (str): Destination path on the device.
    
        Returns:
            None
        rb)source_pathNz$Success pushed, time used %d seconds)
rW   rT   rU   r   r   rC   rS   pushr   ra   )r,   re   r`   r9   rc   lengthfdrs           r    rR   zInstallExtension._push_apk   s     		((]]4  	/B VXG		q*-		r:.	/ 	:diikE>QRS	/ 	/s    AC		C)T)__name__
__module____qualname__staticmethodrC   r   r!   r   booltypingCallablelistr.   r+   rR    r4   r    r   r      s1   # T  6 "'"'$9=#Tl_"39-__  _ 	_
 !//3%+6_ _" "'"'$9=#Tl%)g$"39-g$g$  g$ 	g$
 !//3%+6g$ g$ #g$RT$ TC T Tr4   r   )__doc__loggingrD   rF   rW   rr   pathlibr   r   r   r   adbutils._interfacesr   adbutils._utilsr   r   adbutils.errorsr	   	getLoggerrm   r   r   ru   r4   r    <module>r}      sO     	     "  / 2 +			8	$fT~ fTr4   